Aby przesłać pliki przez MTP z C#:
Pobierz ten pakiet Nuget: PortableDevices
Dodaj odniesień do tych bibliotek 4 COM:
- PortableDeviceClassExtension
- PortableDeviceConnectApi
- PortableDeviceTypes
- PortableDeviceApi
podejmuje dll pod obj\Debug
i umieścić je w bin\Debug
:
- Interop.PortableDeviceClassExtension.dll
- Interop.PortableDeviceConnectApiLib.dll
- Interop.PortableDeviceTypesLib.dll
- Interop.PortableDeviceApiLib.dll
Teraz można użyć następująca funkcja do listy wszystkich urządzeń, chociaż wydaje się, że FriendlyName
pracy (zwraca pusty ciąg znaków):
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;
}
Następnym krokiem jest uzyskanie zawartości z urządzenia, które odbywa się w taki sposób:
var contents = device.GetContents();