DirCopy.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace HXX.Scanner.Copier
  8. {
  9. /// <summary>
  10. /// 目录拷贝
  11. /// </summary>
  12. class DirCopy
  13. {
  14. /// <summary>
  15. /// 检测源目录是否存在,如果存在则创建对应目标目录
  16. /// </summary>
  17. /// <param name="fml_sDst">目标目录</param>
  18. /// <param name="fml_sSrc">源目录</param>
  19. /// <returns></returns>
  20. public static bool CheckDirs(string fml_sDst, string fml_sSrc)
  21. {
  22. bool ok1, ok2, ok3;
  23. ok1 = Directory.Exists(fml_sDst);
  24. ok2 = Directory.Exists(fml_sSrc);
  25. if (!ok2)
  26. {
  27. LogManager.WriteLog("升级覆盖--源目录不存在" + Environment.NewLine + fml_sSrc);
  28. return false;
  29. }
  30. // 目标目录不存在则进行创建
  31. if (!ok1)
  32. {
  33. try
  34. {
  35. Directory.CreateDirectory(fml_sDst);
  36. ok3 = true;
  37. return true;
  38. }
  39. catch (Exception ex)
  40. {
  41. ok3 = false;
  42. LogManager.WriteLog("升级覆盖--创建目标目录失败" + Environment.NewLine + fml_sDst);
  43. return false;
  44. }
  45. }
  46. else
  47. {
  48. return true;
  49. }
  50. }
  51. /// <summary>
  52. /// 文件夹拷贝
  53. /// 从指定的源目录拷贝所有文件到目标目录
  54. /// 如果目标文件存在则跳过拷贝
  55. /// </summary>
  56. /// <param name="fml_sDst">目标目录</param>
  57. /// <param name="fml_sSrc">源目录</param>
  58. /// <returns></returns>
  59. public static bool Copy(string fml_sDst, string fml_sSrc)
  60. {
  61. if (!CheckDirs(fml_sDst, fml_sSrc))
  62. {
  63. return false;
  64. }
  65. /* 迭代拷贝文件
  66. * 1.将源目录存入列表
  67. * 2.检测当前列表第一项目录是否存在,不存在则创建
  68. * 3.检测当前列表第一项目录下的所有文件,并拷贝到目标文件夹下
  69. * 4.检测当前列表第一项目录下的文件夹,添加到列表
  70. * 5.移除第一项
  71. * 6.循环第2到第4步,直到列表项为0 */
  72. List<string> dirs = new List<string>();
  73. dirs.Add(fml_sSrc);
  74. while (dirs.Count != 0)
  75. {
  76. string dst_dir = dirs[0].Replace(fml_sSrc, fml_sDst);
  77. // 如果目标目录不存在则创建
  78. if (!Directory.Exists(dst_dir))
  79. {
  80. Directory.CreateDirectory(dst_dir);
  81. }
  82. List<string> fnames = new List<string>(Directory.EnumerateFiles(dirs[0]));
  83. foreach (string fname in fnames)
  84. {
  85. FileInfo fi = new FileInfo(fname); //获取文件信息
  86. string dst_name = dst_dir + @"\" + fi.Name;
  87. try
  88. {
  89. // 检测本地是否已经存在该文件,如果存在则跳过
  90. if (!File.Exists(dst_name))
  91. {
  92. File.Copy(fname, dst_name);
  93. }
  94. else
  95. {
  96. Console.WriteLine("文件 {0} 已存在,跳过拷贝。", dst_name);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. LogManager.WriteLog("升级覆盖--拷贝文件错误");
  102. }
  103. }
  104. // 列举当前目录下的子目录列表
  105. List<string> sub_dirs = new List<string>(Directory.EnumerateDirectories(dirs[0]));
  106. dirs.AddRange(sub_dirs);
  107. dirs.RemoveAt(0);
  108. }
  109. return true;
  110. }
  111. /// <summary>
  112. /// 预留 在独立线程内进行拷贝
  113. /// </summary>
  114. /// <param name="fml_sDst"></param>
  115. /// <param name="fml_sSrc"></param>
  116. /// <returns></returns>
  117. public static bool threadCopy(string fml_sDst, string fml_sSrc)
  118. {
  119. return false;
  120. }
  121. }
  122. }