public OpenFolderRoot GetFavorites() { OpenFolderRoot root = new OpenFolderRoot(); root.Name = "Favorites"; root.Image = FileInfoHelper.GetFavoritesImage(false); root.Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Links"); if (Directory.Exists(root.Path)) { List <OpenFolderItem> items = new List <OpenFolderItem>(); foreach (string file in Directory.GetFiles(root.Path)) { if (ShortcutHelper.IsShortcut(file)) { OpenFolderItem item = new OpenFolderItem(); string resolved = ShortcutHelper.ResolveShortcut(file); item.Path = resolved; item.Name = Path.GetFileNameWithoutExtension(file); item.Image = FileInfoHelper.GetImage(file, false); items.Add(item); } } root.Children = items; } return(root); }
private async Task DirectoryCopy(string srcPath, string dstPath) { var dirs = Directory.EnumerateDirectories(srcPath); var files = Directory.EnumerateFiles(srcPath); // Does not process linked subdirectories yet! foreach (string filename in files) { string path = filename; if (chkBoxDereferenceLinks.Checked && ShortcutHelper.IsShortcut(filename)) { path = ShortcutHelper.ResolveShortcut(filename); if (!File.Exists(path)) // Ignore bad link { continue; } } using (FileStream SourceStream = File.Open(path, FileMode.Open)) { using (FileStream DestinationStream = File.Create(dstPath + path.Substring(path.LastIndexOf('\\')))) { await SourceStream.CopyToAsync(DestinationStream); fileCount++; } } } // Now copy the subdirectories recursively foreach (string path in dirs) { string dirName = path.Substring(path.LastIndexOf('\\')); string fullDirName = dstPath + dirName; // string tmpPath = Path.Combine(dstPath, path.Substring(path.LastIndexOf('\\'))); // If the subdirectory doesn't exist, create it. if (!Directory.Exists(fullDirName)) { Directory.CreateDirectory(fullDirName); } await DirectoryCopy(path, fullDirName); } }
private static async Task <DirInfo> asyncDirectoryCopy(string srcDir, string dstDir, DirInfo info, Action <DirInfo> progressCallback) { DirInfo inf = info; // Get the subdirectories for the specified directory. var directories = new List <string>(Directory.GetDirectories(srcDir)); // inf.totalDirs += directories.Count(); //var dirs = Directory.EnumerateDirectories(srcDir); var files = Directory.EnumerateFiles(srcDir); int n = files.Count(); foreach (string filename in files) { string srcFile = filename; string dstFile; if (ShortcutHelper.IsShortcut(filename)) { srcFile = ShortcutHelper.ResolveShortcut(filename); string extension = Path.GetExtension(srcFile); if (extension == String.Empty) { MessageBox.Show(String.Format("File {0} is a linked directory", srcFile)); //string path = ShortcutHelper.ResolveShortcut(file); directories.Add(srcFile); continue; } } if (!File.Exists(srcFile)) { // This file had a bad or missing target link // MessageBox.Show(String.Format("File {0} has a bad link", filename)); info.badLinks.Add(filename); } else { dstFile = dstDir + srcFile.Substring(srcFile.LastIndexOf('\\')); // Check to see if destination file exists. // If it does skip it unless overwrite option is true. if (!File.Exists(dstFile) || info.dirOpts.HasFlag(Options.OverWriteFiles)) { using (FileStream SourceStream = File.Open(srcFile, FileMode.Open)) { using (FileStream DestinationStream = File.Create(dstDir + srcFile.Substring(srcFile.LastIndexOf('\\')))) { await SourceStream.CopyToAsync(DestinationStream); } } FileInfo f = new FileInfo(dstFile); info.totalBytes += f.Length; info.totalFiles++; } //FileInfo f = new FileInfo(dstFile); //info.totalBytes += f.Length; //info.totalFiles++; } //info.totalFiles++; progressCallback(inf); } // Now copy the subdirectories recursively if (!info.dirOpts.HasFlag(Options.TopDirectoryOnly)) { inf.totalDirs += directories.Count(); foreach (string path in directories) { string dirName = path.Substring(path.LastIndexOf('\\')); string fullDirName = dstDir + dirName; // If the subdirectory doesn't exist, create it. if (!Directory.Exists(fullDirName)) { Directory.CreateDirectory(fullDirName); } inf = await asyncDirectoryCopy(path, fullDirName, inf, progressCallback); } } return(inf); }