How to Manage Files on an Mtp Portable Device

How to create folder in MTP device by c# windows application?

I referred to the portable device code written in C++.
Three keys are required to create a folder.

  1. WPD_OBJECT_CONTENT_TYPE = WPD_CONTENT_TYPE_FOLDER
  2. WPD_OBJECT_PARENT_ID
  3. WPD_OBJECT_NAME
public void createFolder(string folderName, string parentObjectId)
{
IPortableDeviceContent content;
this._device.Content(out content);

string objectID = null;

IPortableDeviceValues values = GetRequiredPropertiesForFolderType(folderName, parentObjectId);
content.CreateObjectWithPropertiesOnly(values, objectID);
}

private IPortableDeviceValues GetRequiredPropertiesForFolderType(string folderName, string parentObjectId)
{
IPortableDeviceValues values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

//type
var WPD_OBJECT_CONTENT_TYPE = new _tagpropertykey();
WPD_OBJECT_CONTENT_TYPE.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_CONTENT_TYPE.pid = 7;

var WPD_CONTENT_TYPE_FOLDER = new _tagpropertykey();
WPD_CONTENT_TYPE_FOLDER.fmtid = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C, 0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
values.SetGuidValue(ref WPD_OBJECT_CONTENT_TYPE, WPD_CONTENT_TYPE_FOLDER.fmtid);

var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3;
values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

//name
var WPD_OBJECT_NAME = new _tagpropertykey();
WPD_OBJECT_NAME.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_NAME.pid = 4;
values.SetStringValue(WPD_OBJECT_NAME, folderName);

return values;
}

How to access an MTP USB device with python

One way to do this would be to install ADB (android debugging bridge, part of the SDK) and launch it as a child process from python. ADB can be used to, among other things, read from or write to, an android device.

Windows Portable Device - Notify when a new file is created / copied / deleted

WPD_EVENT_OBJECT_ADDED corresponds to MTP event ObjectAdded.
https://msdn.microsoft.com/en-us/microsoft-r/dn932706

I think that what events are really sent depends on concrete implementation of MTP protocol on concrete portable device.

I beleave that WPD API main purpose is to manage portable devices like a phones and music players, access and methods to work with usb flash drives are only exported as some layer of compatibility, and may not contain all functions.

If you want to receive such notifications not from portable devices implements MTP, but only from USB flash drives, you should take a look for example on filesystem minifilters
https://msdn.microsoft.com/en-us/windows/hardware/drivers/ifs/file-system-minifilter-drivers

They could give you needed callbacks.



Related Topics



Leave a reply



Submit