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 static DirInfo GetAllDirInfo(string srcPath, DirInfo info) { DirInfo inf = info; // Get the subdirectories for the specified directory. var directories = new List <string>(Directory.GetDirectories(srcPath)); //inf.totalDirs += directories.Count(); var files = Directory.EnumerateFiles(srcPath); foreach (var file in files) { FileInfo currentFile = new FileInfo(file); if (currentFile.Extension == @".lnk") { // Change the current file info to the linked target file currentFile = new FileInfo(ShortcutHelper.ResolveShortcut(file)); // Check to see if file is a directory if (currentFile.Extension == String.Empty) { MessageBox.Show(String.Format("File {0} is a linked directory", file)); if (true) { string path = ShortcutHelper.ResolveShortcut(file); directories.Add(path); continue; } } if (!currentFile.Exists) { // This file had a bad or missing target link // MessageBox.Show(String.Format("File {0} does not exist", currentFile.FullName)); info.badLinks.Add(file); //info.totalFiles++; // Count it anyway continue; } } info.totalFiles++; info.totalBytes += (long)currentFile.Length; } // Now do the subdirectories if (!info.dirOpts.HasFlag(Options.TopDirectoryOnly)) { inf.totalDirs += directories.Count(); foreach (string path in directories) { //inf.totalFiles = countAllFiles(path, inf.totalFiles); inf = GetAllDirInfo(path, inf); } } return(inf); }
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 void RefreshList() { #if DEBUG var stop = new Stopwatch(); stop.Start(); #endif _applistData.Clear(); GC.Collect(); _currentId = -1; //获取所有子目录内容 //只监视.lnk文件 var fileList = new List <string>(); fileList.AddRange(Helper.GetAllFilesByDir(App.StartMenu)); fileList.AddRange(Helper.GetAllFilesByDir(App.CommonStartMenu)); fileList.RemoveAll(str => !str.EndsWith(".lnk", StringComparison.CurrentCultureIgnoreCase)); foreach (var item in fileList) { var target = ShortcutHelper.ResolveShortcut(item); //__tf: #if DEBUG_SHOW_DETAILS Debug.WriteLine(target); #endif if ((!target.EndsWith(".exe", StringComparison.CurrentCultureIgnoreCase)) || !File.Exists(target)) { #if DEBUG_SHOW_DETAILS Debug.WriteLine("Torow!!!"); #endif continue; } var si = new StartmenuShortcutInfo(item, target); si.LoadIcon(); _applistData.Add(si); } _applistData.Sort(); #if DEBUG stop.Stop(); Debug.WriteLine("Refresh list take:" + stop.Elapsed + " ms"); #endif }
/// <summary> /// 获取现有快捷方式的信息 /// </summary> /// <param name="shortcutFileName"></param> // ReSharper disable once UnusedMember.Global public StartmenuShortcutInfo(string shortcutFileName) { FullPath = shortcutFileName; TargetPath = ShortcutHelper.ResolveShortcut(FullPath); //__find: if (!File.Exists(TargetPath)) { throw new FileNotFoundException(TargetPath); } XmlFileLocation = Path.Combine(Path.GetDirectoryName(TargetPath), Path.GetFileNameWithoutExtension(TargetPath)) + XML_FILE_SIGN; AppName = Path.GetFileNameWithoutExtension(shortcutFileName); // ReSharper disable once AssignNullToNotNullAttribute if (App.charMap_Cn.ContainsKey(AppName)) { AppName = App.charMap_Cn[AppName]; } }
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); }