FileOperation.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. //开始拷贝文件,true表示覆盖同名文件
  173. File.Copy(
  174. strFiles[i],
  175. copyToPath,
  176. true);
  177. }
  178. }
  179. catch { }
  180. }
  181. }
  182. //创建DirectoryInfo实例
  183. DirectoryInfo dirInfo =
  184. new DirectoryInfo(strFromPath);
  185. //取得源文件夹下的所有子文件夹名称
  186. DirectoryInfo[] ZiPath =
  187. dirInfo.GetDirectories();
  188. for (int j = 0; j < ZiPath.Length; j++)
  189. {
  190. //获取所有子文件夹名
  191. string strZiPath = strFromPath + "\\" +
  192. ZiPath[j].ToString();
  193. //把得到的子文件夹当成新的
  194. //源文件夹,从头开始新一轮的拷贝
  195. CopyFolder(
  196. strZiPath,
  197. strToPath + "\\" + strFolderName);
  198. }
  199. }
  200. /// <summary>
  201. /// 实现一个静态方法将指定文件夹下面的所有
  202. /// 内容copy到目标文件夹下面
  203. /// 如果目标文件夹为只读属性就会报错
  204. /// </summary>
  205. /// <param name="srcPath">源文件件</param>
  206. /// <param name="aimPath">目的文件夹</param>
  207. public void CopyDir(string srcPath, string aimPath)
  208. {
  209. try
  210. {
  211. //检查目标目录是否以目录分割字符
  212. //结束如果不是则添加之
  213. if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
  214. aimPath += Path.DirectorySeparatorChar;
  215. //判断目标目录是否存在如果不存在则新建之
  216. if (!Directory.Exists(aimPath))
  217. Directory.CreateDirectory(aimPath);
  218. //得到源目录的文件列表,该里面是包含
  219. //文件以及目录路径的一个数组
  220. //如果你指向copy目标文件下面的文件
  221. //而不包含目录请使用下面的方法
  222. //string[]fileList= Directory.GetFiles(srcPath);
  223. string[] fileList =
  224. Directory.GetFileSystemEntries(srcPath);
  225. //遍历所有的文件和目录
  226. foreach (string file in fileList)
  227. {
  228. //先当作目录处理如果存在这个
  229. //目录就递归Copy该目录下面的文件
  230. if (Directory.Exists(file))
  231. CopyDir(
  232. file,
  233. aimPath + Path.GetFileName(file));
  234. //否则直接Copy文件
  235. else
  236. File.Copy(
  237. file,
  238. aimPath + Path.GetFileName(file),
  239. true);
  240. }
  241. }
  242. catch (Exception e)
  243. {
  244. throw new Exception(e.Message);
  245. }
  246. }
  247. #region 增加文件操作方法 2017-03-17 杨云奕 添加
  248. /// <summary>
  249. /// 检查文件夹是否存在
  250. /// </summary>
  251. /// <param name="dirPath"></param>
  252. /// <returns></returns>
  253. public static bool CheckAndCreateDirectory(string dirPath)
  254. {
  255. try
  256. {
  257. if (!Directory.Exists(dirPath))
  258. {
  259. Directory.CreateDirectory(dirPath);
  260. }
  261. return true;
  262. }
  263. catch {
  264. return false;
  265. }
  266. }
  267. /// <summary>
  268. /// 读取文件
  269. /// </summary>
  270. /// <param name="filePaht"></param>
  271. /// <returns></returns>
  272. public static string ReadFile(string filePaht)
  273. {
  274. if (File.Exists(filePaht))
  275. {
  276. return File.ReadAllText(filePaht);
  277. }
  278. else
  279. {
  280. return "";
  281. }
  282. }
  283. /// <summary>
  284. /// 写入文件
  285. /// </summary>
  286. /// <param name="filePath"></param>
  287. /// <param name="txt"></param>
  288. public static void WriteFile(string filePath,string txt)
  289. {
  290. string dir = Path.GetDirectoryName(filePath);
  291. if(CheckAndCreateDirectory(dir))
  292. {
  293. File.WriteAllText(filePath, txt);
  294. }
  295. }
  296. /// <summary>
  297. /// 写入附加文件
  298. /// </summary>
  299. /// <param name="filePath"></param>
  300. /// <param name="txt"></param>
  301. public static void WriteAppendFile(string filePath, string txt)
  302. {
  303. string dir = Path.GetDirectoryName(filePath);
  304. if (CheckAndCreateDirectory(dir))
  305. {
  306. File.AppendAllText(filePath, txt);
  307. }
  308. }
  309. /// <summary>
  310. /// 文件夹中的文件
  311. /// </summary>
  312. /// <param name="dir"></param>
  313. /// <returns></returns>
  314. public static string[] GetDirectoryFile(string dir)
  315. {
  316. if (!Directory.Exists(dir))
  317. {
  318. CheckAndCreateDirectory(dir);
  319. }
  320. return Directory.GetFiles(dir);
  321. }
  322. #endregion
  323. }
  324. }