简体   繁体   中英

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

I'm trying to transfer files to android MTP device in windows app made with C#. How do i create folder on MTP device in windows app?

I'm using WPD API and I succeeded in sending the file to the MTP device.

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 ;

I read above code in PortableDevices API. I don'n now what guid means.

I thought api would support the function to create folders. But, However, api support only reading, writing and deleting files.

How do i create folrder on MTP device? Or, is there any good C# api for mtp device?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM