public static void DeleteFolderVincapture(PortableDevice device, PortableDeviceFolder vinFolder) { foreach (var tmp in vinFolder.Files) { if (tmp is PortableDeviceFolder) { DeleteFolderVincapture(device, (PortableDeviceFolder)tmp); var tmpFile = (PortableDeviceFolder)tmp; device.DeleteFolder(tmpFile); } if (tmp is PortableDeviceFile) { var tmpFile = (PortableDeviceFile)tmp; device.DeleteFile(tmpFile); } } }
static void Main() { //Connect to MTP devices and pick up the first one var devices = new PortableDeviceCollection(); devices.Refresh(); Tablet = devices.First(); Tablet.Connect(); //Getting root directory var root = Tablet.GetContents(); //Displayinh directory content in console foreach (var resource in root.Files) { DisplayResourceContents(resource); } //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); //Transfering file from stream into device folder var imgPath = "\\LOCALPATH"; var image = Image.FromFile(imgPath); byte[] imageB; using (var ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); imageB = ms.ToArray(); } Tablet.TransferFileToDeviceFromStream("FILE NAME", new MemoryStream(imageB), folder.Id); //Close the connection Tablet.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }