/// <summary> /// Download a file from a portable device. /// </summary> /// <param name="device">Device class.</param> /// <param name="source">The path to the source.</param> /// <param name="destination">The path to the destination.</param> /// <exception cref="System.IO.IOException">path is a file name.</exception> /// <exception cref="System.ArgumentException">path is a zero-length string, contains only white space, or contains invalid characters as defined by System.IO.Path.GetInvalidPathChars.</exception> /// <exception cref="System.ArgumentNullException">path is null.</exception> /// <exception cref="System.IO.DirectoryNotFoundException">path is invalid.</exception> /// <exception cref="MediaDevices.NotConnectedException">device is not connected.</exception> public static void DownloadFile(this MediaDevice device, string source, string destination) { if (source == null) { throw new ArgumentNullException("source"); } if (!MediaDevice.IsPath(source)) { throw new ArgumentException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } if (!MediaDevice.IsPath(destination)) { throw new ArgumentException("destination"); } if (!device.IsConnected) { throw new NotConnectedException("Not connected"); } using (FileStream stream = File.Create(destination)) { device.DownloadFile(source, stream); } }
/// <summary> /// Execute -cp action for file f. Update database if needed. /// </summary> private void DownloadFiles(MediaDevice device, MediaFileInfo f, Dictionary <string, FileSpec> database) { var localFilename = Path.Combine(localFolder, f.Name.Substring(f.Name.LastIndexOf("\\") + 1)); Console.Error.WriteLine("writing: " + localFilename); // DEBUG msg FileStream fs = File.Create(localFilename); device.DownloadFile(f.FullName, fs); FileSpec fspec = null; if (removeDuplicates || splitFolders) { fspec = new FileSpec(localFilename, f.CreationTime); } if (removeDuplicates) { if (database.ContainsKey(fspec.ContentHash())) { Console.Error.WriteLine("ignoring duplicate file: " + localFilename); File.Delete(localFilename); } else { database[fspec.ContentHash()] = fspec; } } }
private void SaveMode1(List <MediaFileInfo> files, string path) { progressBar1.Invoke(new SetBarStyleDelegate(SetBarStyle), ProgressBarStyle.Blocks); progressBar1.Invoke(new SetBarValueDelegate(SetBarMaxValue), files.Count); label1.Invoke(new SetTextDelegate(SetText), string.Format("Speed: {0}\nFiles copied: {1}/{2}", "unknown", 0, files.Count)); foreach (MediaFileInfo m in files) { string filePath = Path.Combine(path, Guid.NewGuid().ToString() + Path.GetExtension(m.Name)); FileStream s = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write); Device.DownloadFile(m.FullName, s); //Utils.WriteSreamToDisk(diskPath, s); s.Flush(); s.Close(); progressBar1.Invoke(new SetBarValueDelegate(SetBarValue), progressBar1.Value + 1); label1.Invoke(new SetTextDelegate(SetText), string.Format("Speed: {0}\nFiles copied: {1}/{2}", "unknown", progressBar1.Value, files.Count)); label1.Text = string.Format("Speed: {0}\nFiles copied: {1}/{2}", "unknown", progressBar1.Value, files.Count); } }
public void DownloadFile(MediaFileInfo sourceFile, string destinationFolderPath) { MemoryStream memoryStream = new MemoryStream(); _connectedDevice.DownloadFile(sourceFile.FullName, memoryStream); memoryStream.Position = 0; using (FileStream file = new FileStream($@"{destinationFolderPath}\{sourceFile.Name}", FileMode.Create, FileAccess.Write)) { byte[] bytes = new byte[memoryStream.Length]; memoryStream.Read(bytes, 0, (int)memoryStream.Length); file.Write(bytes, 0, bytes.Length); memoryStream.Close(); } }
private static int RunCommand(GetOption opts) { using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts)) { if (device != null) { device.Connect(); device.DownloadFile(opts.Source, opts.Destination); device.Disconnect(); } else { Console.WriteLine("No device connected."); return(1); } } return(0); }
public Task CopyDeviceFileToDesktop(string sourceDeviceFilePath, string destinationDesktopFilePath) { return(Task.Run(() => { Monitor.Enter(this); try { sourceDeviceFilePath = GetAbsoluteDevicePath(sourceDeviceFilePath); string directory = Path.GetDirectoryName(destinationDesktopFilePath); if (directory != null) { Directory.CreateDirectory(directory); } device.DownloadFile(sourceDeviceFilePath, destinationDesktopFilePath); } finally { Monitor.Exit(this); } })); }