Để chuyển các tập tin thông qua MTP với C#:
Tải gói NuGet này: PortableDevices
Thêm tham chiếu đến các thư viện 4 COM:
- PortableDeviceClassExtension
- PortableDeviceConnectApi
- PortableDeviceTypes
- PortableDeviceApi
Lấy của dll dưới obj\Debug
và đặt chúng vào bin\Debug
:
- Interop.PortableDeviceClassExtension.dll
- Interop.PortableDeviceConnectApiLib.dll
- Interop.PortableDeviceTypesLib.dll
- Interop.PortableDeviceApiLib.dll
Bây giờ bạn có thể sử dụng chức năng sau đây để liệt kê tất cả các thiết bị, mặc dù FriendlyName
dường như không làm việc (nó sẽ trả về một chuỗi rỗng):
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;
}
Bước tiếp theo là nhận được các nội dung từ các thiết bị, được thực hiện như sau:
var contents = device.GetContents();
Nguồn
2016-10-02 15:07:46