PortableDevices.Plus is a .NET wrapper around WIN32_API classes, which allows you to communicate with music players, storage devices, mobile phones, cameras and many other types of connected devices the easy way.
To transfer files, create folders and make other wondrous things through MTP with C#:
PortableDevices.Plus.x64 or PortableDevices.Plus.x86
- PortableDeviceClassExtension
- PortableDeviceConnectApi
- PortableDeviceTypes
- PortableDeviceApi
Take the following dll's under obj\Debug (or get them from CompiledDLLs folder in this repo) and put them into bin\Debug:
- Interop.PortableDeviceClassExtension.dll
- Interop.PortableDeviceConnectApiLib.dll
- Interop.PortableDeviceTypesLib.dll
- Interop.PortableDeviceApiLib.dll
//Creating instances
PortableDevice tablet;
PortableDeviceCollection devices = new PortableDeviceCollection();
devices.Refresh();
//Connect to MTP devices and pick up the first one
tablet = devices.First();
tablet.Connect();
//Getting root directory
var root = tablet.GetContents();
//Finding neccessary folder inside the root
var folder = (root.Files.FirstOrDefault() as PortableDeviceFolder).
Files.FirstOrDefault(x => x.Name == "Folder") as PortableDeviceFolder;
//Finding file inside the folder
var file = (folder as PortableDeviceFolder)?.Files?.FirstOrDefault(x => x.Name == "File");
//Deleting file device-side
tablet.DeleteFile(file as PortableDeviceFile);
//Transfering file into byte array
var fileIntoByteArr = tablet.DownloadFileToStream(file as PortableDeviceFile);
//Transfering file into file system
tablet.DownloadFile(file as PortableDeviceFile, "\\LOCALPATH");
//Transfering file from file system into device folder
tablet.TransferFileToDevice("\\LOCALPATH", folder.Id);
//Creating folder device-side
tablet.CreateFolder("FOLDER NAME", folder.Id);
//Deleting folder device-side
tablet.DeleteFolder(folder);
//Close the connection
tablet.Disconnect();
MIT