Files.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using CommonLang.Concurrent;
  6. namespace CommonLang.File
  7. {
  8. public static class CFiles
  9. {
  10. public static List<FileInfo> listAllFiles(DirectoryInfo dir)
  11. {
  12. List<FileInfo> list = new List<FileInfo>();
  13. listAllFiles(list, dir);
  14. return list;
  15. }
  16. public static void listAllFiles(List<FileInfo> list, DirectoryInfo dir)
  17. {
  18. foreach (FileInfo sub in dir.GetFiles())
  19. {
  20. list.Add(sub);
  21. }
  22. foreach (DirectoryInfo sub in dir.GetDirectories())
  23. {
  24. listAllFiles(list, sub);
  25. }
  26. }
  27. public static void createFile(FileInfo file)
  28. {
  29. createDir(file.Directory);
  30. }
  31. public static void createDir(DirectoryInfo dir)
  32. {
  33. Stack<DirectoryInfo> stack = new Stack<DirectoryInfo>(1);
  34. while (!dir.Exists)
  35. {
  36. stack.Push(dir);
  37. dir = dir.Parent;
  38. }
  39. while (stack.Count > 0)
  40. {
  41. DirectoryInfo d = stack.Pop();
  42. d.Create();
  43. }
  44. }
  45. public static bool fileEquals(FileInfo a, FileInfo b)
  46. {
  47. return a.FullName.Equals(b.FullName);
  48. }
  49. public static void FileCopy(string srcFile, string dstFile, bool overwrite)
  50. {
  51. FileInfo df = new FileInfo(dstFile);
  52. createDir(df.Directory);
  53. System.IO.File.Copy(srcFile, dstFile, overwrite);
  54. }
  55. public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true, Filter filter = null, AtomicInteger progress = null)
  56. {
  57. // Get the subdirectories for the specified directory.
  58. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  59. DirectoryInfo[] dirs = dir.GetDirectories();
  60. if (!dir.Exists)
  61. {
  62. throw new DirectoryNotFoundException(
  63. "Source directory does not exist or could not be found: "
  64. + sourceDirName);
  65. }
  66. // If the destination directory doesn't exist, create it.
  67. if (!Directory.Exists(destDirName))
  68. {
  69. Directory.CreateDirectory(destDirName);
  70. }
  71. // Get the files in the directory and copy them to the new location.
  72. FileInfo[] files = dir.GetFiles();
  73. foreach (FileInfo file in files)
  74. {
  75. if (filter != null)
  76. {
  77. bool ignore = false;
  78. filter(file, out ignore);
  79. if (ignore)
  80. {
  81. progress.IncrementAndGet();
  82. continue;
  83. }
  84. }
  85. string temppath = Path.Combine(destDirName, file.Name);
  86. file.CopyTo(temppath, false);
  87. progress.IncrementAndGet();
  88. }
  89. // If copying subdirectories, copy them and their contents to new location.
  90. if (copySubDirs)
  91. {
  92. foreach (DirectoryInfo subdir in dirs)
  93. {
  94. string temppath = Path.Combine(destDirName, subdir.Name);
  95. DirectoryCopy(subdir.FullName, temppath, copySubDirs, filter, progress);
  96. }
  97. }
  98. }
  99. public delegate void Filter(FileInfo src, out bool ignore);
  100. }
  101. }