C# を使用して MTP 経由でファイルを転送するには:
この NuGet パッケージをダウンロードします: PortableDevices
次の 4 つの COM ライブラリへの参照を追加します。
- PortableDeviceClassExtension
- PortableDeviceConnectApi
- PortableDeviceTypes
- PortableDeviceApi
dll を取得して、次の場所obj\Debug
に配置しますbin\Debug
。
- Interop.PortableDeviceClassExtension.dll
- Interop.PortableDeviceConnectApiLib.dll
- Interop.PortableDeviceTypesLib.dll
- Interop.PortableDeviceApiLib.dll
次の関数を使用してすべてのデバイスを一覧表示できますが、機能してFriendlyName
いないようです (空の文字列が返されます)。
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;
}
次のステップは、デバイスからコンテンツを取得することです。これは次のように行われます。
var contents = device.GetContents();