FileOperation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace LYFZ.Network.FileOperations
  7. {
  8. /// <summary>
  9. /// 文件操作类
  10. /// </summary>
  11. public class FileOperation
  12. {
  13. /// <summary>
  14. /// 删除文件夹下所有文件
  15. /// </summary>
  16. /// <param name="dir"></param>
  17. public bool DeleteFolder(string dir) {
  18. string _message = "";
  19. return DeleteFolder(dir, out _message);
  20. }
  21. /// <summary>
  22. /// 删除文件夹下所有文件
  23. /// </summary>
  24. /// <param name="dir"></param>
  25. /// <param name="message"></param>
  26. /// <returns></returns>
  27. public bool DeleteFolder(string dir,out string message)
  28. {
  29. bool bl = false;
  30. //如果存在这个文件夹删除之
  31. if (Directory.Exists(dir))
  32. {
  33. foreach (string d in Directory.GetFileSystemEntries(dir))
  34. {
  35. if (File.Exists(d))
  36. File.Delete(d);//直接删除其中的文件
  37. else DeleteFolder(d, out message);//递归删除子文件夹
  38. }
  39. Directory.Delete(dir);
  40. //删除已空文件夹
  41. message = (dir + "文件夹删除成功");
  42. bl = true;
  43. }
  44. else
  45. {//如果文件夹不存在则提示
  46. message = (dir + "该文件夹不存在");
  47. bl = false;
  48. }
  49. return bl;
  50. }
  51. /// <summary>
  52. /// 实现一个静态方法将指定文件夹下面的所有内容Detele
  53. /// 测试的时候要小心操作,删除之后无法恢复。
  54. /// </summary>
  55. /// <param name="aimPath"></param>
  56. /// <param name="isDeleteOwn">是否删除指定文件夹自己</param>
  57. /// <returns></returns>
  58. public static void DeleteDir(string aimPath, bool isDeleteOwn=false)
  59. {
  60. try
  61. {
  62. //检查目标目录是否以目录分割字符结束如果不是则添加之
  63. if (aimPath[aimPath.Length - 1] !=
  64. Path.DirectorySeparatorChar)
  65. aimPath += Path.DirectorySeparatorChar;
  66. //得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
  67. //如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
  68. //string[]fileList= Directory.GetFiles(aimPath);
  69. string[] fileList = Directory.GetFileSystemEntries(aimPath);
  70. //遍历所有的文件和目录
  71. foreach (string file in fileList)
  72. {
  73. //先当作目录处理如果存在这个
  74. //目录就递归Delete该目录下面的文件
  75. if (Directory.Exists(file))
  76. {
  77. DeleteDir(aimPath + Path.GetFileName(file));
  78. }
  79. //否则直接Delete文件
  80. else
  81. {
  82. File.Delete(aimPath + Path.GetFileName(file));
  83. }
  84. }
  85. //删除文件夹
  86. if (!isDeleteOwn)
  87. {
  88. DirectoryInfo dirInfo = new DirectoryInfo(aimPath);
  89. //取得源文件夹下的所有子文件夹名称
  90. DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
  91. for (int j = 0; j < ZiPath.Length; j++)
  92. {
  93. //获取子文件夹名
  94. string strZiPath = aimPath + "\\" + ZiPath[j].ToString();
  95. //删除当前文件夹
  96. System.IO.Directory.Delete(strZiPath, true);
  97. }
  98. }
  99. else
  100. {
  101. System.IO.Directory.Delete(aimPath, true);
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. throw new Exception(e.Message);
  107. }
  108. }
  109. /// <summary>
  110. /// 拷贝文件夹(包括子文件夹)到指定文件夹下
  111. /// 文件和文件夹分开复制,当是文件夹则递归复制
  112. ///源文件夹和目标文件夹均需绝对路径
  113. ///格式:CopyFolder(源文件夹,目标文件夹)
  114. /// </summary>
  115. /// <param name="strFromPath">待复制地址</param>
  116. /// <param name="strToPath">目标地址</param>
  117. public static void CopyFolder(string strFromPath, string strToPath, bool overwrite = false)
  118. {
  119. //如果源文件夹不存在,则创建
  120. if (!Directory.Exists(strFromPath))
  121. {
  122. Directory.CreateDirectory(strFromPath);
  123. }
  124. //取得要拷贝的文件夹名
  125. string strFolderName = strFromPath.Substring(
  126. strFromPath.LastIndexOf("\\") + 1,
  127. strFromPath.Length -
  128. strFromPath.LastIndexOf("\\") - 1);
  129. //如果目标文件夹中没有源文件夹
  130. //则在目标文件夹中创建源文件夹
  131. if (!Directory.Exists(
  132. strToPath + "\\" + strFolderName))
  133. {
  134. Directory.CreateDirectory(
  135. strToPath + "\\" + strFolderName);
  136. }
  137. //创建数组保存源文件夹下的文件名
  138. string[] strFiles =
  139. Directory.GetFiles(strFromPath);
  140. //循环拷贝文件
  141. for (int i = 0; i < strFiles.Length; i++)
  142. {
  143. //取得拷贝的文件名,只取文件名,地址截掉。
  144. string strFileName = strFiles[i].Substring(
  145. strFiles[i].LastIndexOf("\\") + 1,
  146. strFiles[i].Length -
  147. strFiles[i].LastIndexOf("\\") - 1);
  148. string copyToPath = strToPath + "\\" + strFolderName +
  149. "\\" + strFileName;
  150. if (overwrite || !System.IO.File.Exists(copyToPath))
  151. {
  152. //开始拷贝文件,true表示覆盖同名文件
  153. File.Copy(
  154. strFiles[i],
  155. copyToPath,
  156. true);
  157. }
  158. else if (System.IO.File.Exists(copyToPath))
  159. {
  160. try
  161. {
  162. string fileMd51 = LYFZ.WinAPI.SDKSecurity.GetMD5HashFromFile(copyToPath);
  163. string fileMd52 = LYFZ.WinAPI.SDKSecurity.GetMD5HashFromFile(strFiles[i]);
  164. if (fileMd51 != fileMd52)
  165. {
  166. try
  167. {
  168. System.IO.File.Move(copyToPath, strToPath + "\\" + strFolderName + "\\[oldfile_" + DateTime.Now.ToString("yyyy-MM-dd") + "]" + strFileName);
  169. }
  170. catch
  171. {
  172. }
  173. //开始拷贝文件,true表示覆盖同名文件
  174. File.Copy(
  175. strFiles[i],
  176. copyToPath,
  177. true);
  178. }
  179. }
  180. catch { }
  181. }
  182. }
  183. //创建DirectoryInfo实例
  184. DirectoryInfo dirInfo =
  185. new DirectoryInfo(strFromPath);
  186. //取得源文件夹下的所有子文件夹名称
  187. DirectoryInfo[] ZiPath =
  188. dirInfo.GetDirectories();
  189. for (int j = 0; j < ZiPath.Length; j++)
  190. {
  191. //获取所有子文件夹名
  192. string strZiPath = strFromPath + "\\" +
  193. ZiPath[j].ToString();
  194. //把得到的子文件夹当成新的
  195. //源文件夹,从头开始新一轮的拷贝
  196. CopyFolder(
  197. strZiPath,
  198. strToPath + "\\" + strFolderName);
  199. }
  200. }
  201. public static void CopyFile(string srcFile, string toDirPath, bool overwrite = false)
  202. {
  203. //取得拷贝的文件名,只取文件名,地址截掉。
  204. string strFileName = srcFile.Substring(
  205. srcFile.LastIndexOf("\\") + 1,
  206. srcFile.Length -
  207. srcFile.LastIndexOf("\\") - 1);
  208. string copyToPath = toDirPath + "\\" +
  209. "\\" + strFileName;
  210. if (overwrite || !System.IO.File.Exists(copyToPath))
  211. {
  212. //开始拷贝文件,true表示覆盖同名文件
  213. File.Copy(
  214. srcFile,
  215. copyToPath,
  216. true);
  217. }
  218. else if (System.IO.File.Exists(copyToPath))
  219. {
  220. try
  221. {
  222. string fileMd51 = LYFZ.WinAPI.SDKSecurity.GetMD5HashFromFile(copyToPath);
  223. string fileMd52 = LYFZ.WinAPI.SDKSecurity.GetMD5HashFromFile(srcFile);
  224. if (fileMd51 != fileMd52)
  225. {
  226. try
  227. {
  228. System.IO.File.Move(copyToPath, toDirPath + "\\[oldfile_" + DateTime.Now.ToString("yyyy-MM-dd") + "]" + strFileName);
  229. }
  230. catch
  231. {
  232. }
  233. //开始拷贝文件,true表示覆盖同名文件
  234. File.Copy(
  235. srcFile,
  236. copyToPath,
  237. true);
  238. }
  239. }
  240. catch { }
  241. }
  242. }
  243. /// <summary>
  244. /// 实现一个静态方法将指定文件夹下面的所有
  245. /// 内容copy到目标文件夹下面
  246. /// 如果目标文件夹为只读属性就会报错
  247. /// </summary>
  248. /// <param name="srcPath">源文件件</param>
  249. /// <param name="aimPath">目的文件夹</param>
  250. public void CopyDir(string srcPath, string aimPath)
  251. {
  252. try
  253. {
  254. //检查目标目录是否以目录分割字符
  255. //结束如果不是则添加之
  256. if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
  257. aimPath += Path.DirectorySeparatorChar;
  258. //判断目标目录是否存在如果不存在则新建之
  259. if (!Directory.Exists(aimPath))
  260. Directory.CreateDirectory(aimPath);
  261. //得到源目录的文件列表,该里面是包含
  262. //文件以及目录路径的一个数组
  263. //如果你指向copy目标文件下面的文件
  264. //而不包含目录请使用下面的方法
  265. //string[]fileList= Directory.GetFiles(srcPath);
  266. string[] fileList =
  267. Directory.GetFileSystemEntries(srcPath);
  268. //遍历所有的文件和目录
  269. foreach (string file in fileList)
  270. {
  271. //先当作目录处理如果存在这个
  272. //目录就递归Copy该目录下面的文件
  273. if (Directory.Exists(file))
  274. CopyDir(
  275. file,
  276. aimPath + Path.GetFileName(file));
  277. //否则直接Copy文件
  278. else
  279. File.Copy(
  280. file,
  281. aimPath + Path.GetFileName(file),
  282. true);
  283. }
  284. }
  285. catch (Exception e)
  286. {
  287. throw new Exception(e.Message);
  288. }
  289. }
  290. #region 增加文件操作方法 2017-03-17 杨云奕 添加
  291. /// <summary>
  292. /// 检查文件夹是否存在
  293. /// </summary>
  294. /// <param name="dirPath"></param>
  295. /// <returns></returns>
  296. public static bool CheckAndCreateDirectory(string dirPath)
  297. {
  298. try
  299. {
  300. if (!Directory.Exists(dirPath))
  301. {
  302. Directory.CreateDirectory(dirPath);
  303. }
  304. return true;
  305. }
  306. catch {
  307. return false;
  308. }
  309. }
  310. /// <summary>
  311. /// 读取文件
  312. /// </summary>
  313. /// <param name="filePaht"></param>
  314. /// <returns></returns>
  315. public static string ReadFile(string filePaht)
  316. {
  317. if (File.Exists(filePaht))
  318. {
  319. return File.ReadAllText(filePaht);
  320. }
  321. else
  322. {
  323. return "";
  324. }
  325. }
  326. /// <summary>
  327. /// 写入文件
  328. /// </summary>
  329. /// <param name="filePath"></param>
  330. /// <param name="txt"></param>
  331. public static void WriteFile(string filePath,string txt)
  332. {
  333. string dir = Path.GetDirectoryName(filePath);
  334. if(CheckAndCreateDirectory(dir))
  335. {
  336. File.WriteAllText(filePath, txt);
  337. }
  338. }
  339. /// <summary>
  340. /// 写入附加文件
  341. /// </summary>
  342. /// <param name="filePath"></param>
  343. /// <param name="txt"></param>
  344. public static void WriteAppendFile(string filePath, string txt)
  345. {
  346. string dir = Path.GetDirectoryName(filePath);
  347. if (CheckAndCreateDirectory(dir))
  348. {
  349. File.AppendAllText(filePath, txt);
  350. }
  351. }
  352. /// <summary>
  353. /// 文件夹中的文件
  354. /// </summary>
  355. /// <param name="dir"></param>
  356. /// <returns></returns>
  357. public static string[] GetDirectoryFile(string dir)
  358. {
  359. if (!Directory.Exists(dir))
  360. {
  361. CheckAndCreateDirectory(dir);
  362. }
  363. return Directory.GetFiles(dir);
  364. }
  365. #endregion
  366. }
  367. }