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"); //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 rom file system into device folder Tablet.TransferContentToDevice("\\LOCALPATH", folder.Id); //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.TransferContentToDeviceFromStream("FILE NAME", new MemoryStream(imageB), folder.Id); //Close the connection Tablet.Disconnect(); Console.WriteLine(); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
private void downloadFiles(PortableDevice device, PortableDeviceFolder folder, String outputFolder) { foreach (var item in folder.Files) { if (item is PortableDeviceFile) { Console.WriteLine("\tDownloading: " + item.Name); device.DownloadFile((PortableDeviceFile)item, outputFolder); } } }
public static void CopyFolderInsideVincapture(PortableDevice device, PortableDeviceFolder vinFolder, string baseUrl) { foreach (var tmp in vinFolder.Files) { if (tmp is PortableDeviceFolder) { var folderPath = baseUrl + "\\" + tmp.Name; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } CopyFolderInsideVincapture(device, (PortableDeviceFolder)tmp, folderPath); } if (tmp is PortableDeviceFile) { var tmpFile = (PortableDeviceFile)tmp; device.DownloadFile(tmpFile, baseUrl); } } }
private void getServicesList(PortableDevice device, PortableDeviceFolder folder) { foreach (var item in folder.Files) { if (item is PortableDeviceFile && item.Name.Equals("services")) { device.DownloadFile((PortableDeviceFile)item, "."); servicesFileDetected = true; break; } } if (File.Exists("services")) { BeginInvoke(new MethodInvoker(delegate { System.IO.StreamReader file = new System.IO.StreamReader("services"); String line; while ((line = file.ReadLine()) != null) { services.Items.Add(line); } file.Close(); })); } }
private async Task CopyFiles(PortableDevice device, IEnumerable <PortableDeviceObject> files, string path, bool delete, string additionalPath, DateMethod dateMethod, string additionalExtension) { if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } int counter = 0; ProgressB.Value = 0; ProgressB.Maximum = files.Count(); DirectoryInfo di = new DirectoryInfo(path); firstF = di.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly).Count(); string[] existingFiles = (from FileInfo f in di.GetFiles("*.*", SearchOption.AllDirectories) select f.Name).ToArray(); PortableDevice pd = collection[DevicesListBox.SelectedIndex]; foreach (var item in files) { if (item is PortableDeviceFile) { PortableDeviceFile file = (PortableDeviceFile)item; try { if (ignoreCheckBox.IsChecked == true) { if (existingFiles.Contains(file.Name + additionalExtension)) { existsCounter++; ProgressMessageLabel.Content = existsCounter.ToString() + " files were already existed. I ignored them."; counter++; ProgressB.Value = counter; continue; } } device.Connect(); Exception taskEx = null; await Task.Factory.StartNew(() => { try { pd.DownloadFile(file, path); } catch (Exception ex) { taskEx = ex; } }); if (taskEx != null) { throw taskEx; } device.Disconnect(); await CopyFile(path, file.Name, dateMethod, additionalExtension, additionalPath); counter++; ProgressB.Value = counter; if (delete) { device.Connect(); await Task.Factory.StartNew(() => { pd.DeleteFile(file); }); device.Disconnect(); } } catch (Exception ex) { //MessageBox.Show("Error while copying " + file.Name + "\n\n" + ex.Message); } } } }