Write Files to WPD device via MTP C#/VB.net
Asked Answered
T

2

12

I would like to write an application that will copy MP3 files to a SanDisk Sansa M240. The SanDisk doesn't have a drive letter and uses MTP for file transfer. I stumbled through the sample of connecting to the device at : http://blogs.msdn.com/dimeby8/archive/2006/09/27/774259.aspx

but once connected, I can't figure out how to actually copy files / create folders on the device.

I am very surprised that there aren't any .Net wrappers for this COM library.

Toinette answered 6/11, 2008 at 0:3 Comment(0)
N
5

It looks like dimeby8 posted some code that handles data transfers. Check other posts in his blog, especially:

Sending MTP commands through WPD part 1

Sending MTP commands through WPD part 2

Sending MTP commands through WPD part 3

Negation answered 31/12, 2008 at 15:37 Comment(0)
D
2

To transfer files through MTP with c#:

  1. Download this NuGet package: PortableDevices

  2. Add references to these 4 COM libraries:

    • PortableDeviceClassExtension
    • PortableDeviceConnectApi
    • PortableDeviceTypes
    • PortableDeviceApi
  3. Take the dll's under obj\Debug and put them into bin\Debug:

    • Interop.PortableDeviceClassExtension.dll
    • Interop.PortableDeviceConnectApiLib.dll
    • Interop.PortableDeviceTypesLib.dll
    • Interop.PortableDeviceApiLib.dll

Now you can use the following function to list all devices, although FriendlyName does not seem to be working (it returns an empty string):

    private IDictionary<string, string> GetDeviceIds()
    {
        var deviceIds = new Dictionary<string, string>();
        var devices = new PortableDeviceCollection();
        devices.Refresh();
        foreach (var device in devices)
        {
            device.Connect();
            deviceIds.Add(device.FriendlyName, device.DeviceId);
            Console.WriteLine(@"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
            device.Disconnect();
        }
        return deviceIds;
    }

The next step is getting the contents from the device, which is done like so:

var contents = device.GetContents();
Dingus answered 2/10, 2016 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.