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); } } } }
private async Task CopyFile(string path, string fileName, DateMethod dateMethod, string additionalExtension, string additionalPath) { string copiedFile = System.IO.Path.Combine(path, fileName /*+ additionalExtension*/); System.IO.FileInfo fi = new System.IO.FileInfo(copiedFile); DateTime d; try { if (dateMethod == DateMethod.DateModified) { d = fi.LastWriteTime; } else if (dateMethod == DateMethod.JpegData) { d = GetDateTakenFromImage(copiedFile); } else /*if (dateMethod == DateMethod.FileName)*/ { CultureInfo persianCulture = new CultureInfo("fa-IR"); string strDate = fileName.Split(new char[] { '_' })[1]; bool persianDate = false; if (int.Parse(strDate.Substring(0, 4)) < 1900) { persianDate = true; } d = DateTime.ParseExact(fileName.Split(new char[] { '_' })[1], "yyyyMMdd", persianDate ? persianCulture : CultureInfo.InvariantCulture); } } catch { if (additionalExtension.Length > 0) { d = new DateTime(1900, 1, 1); } else if (dateMethod == DateMethod.FileName) //Use DateModified { d = fi.LastWriteTime; } else //Try using FileName, but if it's not possible, say UNKNOWN. { //d = fi.LastWriteTime; List <string> s = fileName.Split(new char[] { '_' }).ToList(); if (s.Count > 1) { DateTime.TryParseExact(s[1], "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d); if (d.Year < 1000) { d = new DateTime(1900, 1, 1); } } else { d = new DateTime(1900, 1, 1); } } } string destinationPath; if (d.Year == 1900) { destinationPath = "Unknown"; } else { destinationPath = GetDestinationPath(d); } string completeDestPath; if (DestPathes.ContainsKey(destinationPath)) { if (additionalPath.Length > 0) { completeDestPath = System.IO.Path.Combine(DestPathes[destinationPath], additionalPath); } else { completeDestPath = DestPathes[destinationPath]; } } else { completeDestPath = GetExistingFolderPath(path, destinationPath); if (completeDestPath.Length == 0) { if (d.Year == 1900) { completeDestPath = System.IO.Path.Combine(path, destinationPath); } else { if (prevDestinationPath != destinationPath) { firstF += 1; } completeDestPath = System.IO.Path.Combine(path, firstF.ToString() + " - " + destinationPath); } } DestPathes.Add(destinationPath, completeDestPath); prevDestinationPath = destinationPath; } if (!System.IO.Directory.Exists(completeDestPath)) { System.IO.Directory.CreateDirectory(completeDestPath); } /* We can't determine Creation Date of file before copying it. So, we'll have to copy it anyway. */ int newFileNum = 1; string newFile = System.IO.Path.Combine(completeDestPath, fileName + additionalExtension); while (System.IO.File.Exists(newFile)) { //Assuming that ignoreCheckBox.IsChecked == false , because if it was checked, the code won't reach here. newFileNum++; newFile = System.IO.Path.Combine(completeDestPath, System.IO.Path.GetFileNameWithoutExtension(fileName + additionalExtension) + " (" + newFileNum.ToString() + ")" + System.IO.Path.GetExtension(fileName + additionalExtension)); } await Task.Run(() => { System.IO.File.Move(copiedFile, newFile); }); }
private async Task CopyFiles(IEnumerable <string> 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(); prevDestinationPath = ""; string[] existingFiles = (from FileInfo f in di.GetFiles("*.*", SearchOption.AllDirectories) select f.Name).ToArray(); foreach (var item in files) { try { if (ignoreCheckBox.IsChecked == true) { if (existingFiles.Contains(item + additionalExtension)) { existsCounter++; ProgressMessageLabel.Content = existsCounter.ToString() + " files were already existed. I ignored them."; counter++; ProgressB.Value = counter; continue; } } System.IO.File.Copy(item, System.IO.Path.Combine(path, System.IO.Path.GetFileName(item))); await CopyFile(path, System.IO.Path.GetFileName(item), dateMethod, additionalExtension, additionalPath); counter++; ProgressB.Value = counter; if (delete) { System.IO.File.Delete(item); } } catch (Exception ex) { //MessageBox.Show("Error while copying " + file.Name + "\n\n" + ex.Message); } } }