To transfer files through MTP with c#:
Download this NuGet package: PortableDevices
Add references to these 4 COM libraries:
- PortableDeviceClassExtension
- PortableDeviceConnectApi
- PortableDeviceTypes
- PortableDeviceApi
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();