FileOperate.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. using System.Windows.Forms;
  8. using System.Threading;
  9. namespace BaseLibRWFile
  10. {
  11. public class FileOperate
  12. {
  13. #region folder & file Operate
  14. /// <summary>
  15. /// 判断文件夹是否存在
  16. /// </summary>
  17. /// <param name="folderPath">文件路径</param>
  18. /// <returns></returns>
  19. public static bool IsFolderExist(string folderPath)
  20. {
  21. DirectoryInfo folder = new DirectoryInfo(folderPath);
  22. if (folder.Exists)
  23. return true;
  24. return false;
  25. }
  26. /// <summary>
  27. /// 删除指定文件夹(包括里面的文件)
  28. /// </summary>
  29. /// <param name="folderPath">指定文件夹路径</param>
  30. /// <returns></returns>
  31. public static bool DeleteFolder(string folderPath)
  32. {
  33. try
  34. {
  35. DirectoryInfo dir = new DirectoryInfo(folderPath);
  36. if (dir.Exists)
  37. {
  38. DirectoryInfo[] childs = dir.GetDirectories();//返回当前目录的子目录
  39. foreach (DirectoryInfo child in childs)
  40. {
  41. child.Delete(true);//删除子文件
  42. }
  43. dir.Delete(true);//删除文件夹
  44. }
  45. return true;
  46. }
  47. catch
  48. {
  49. return false;
  50. }
  51. }
  52. /// <summary>
  53. /// 创建文件夹
  54. /// </summary>
  55. /// <param name="DirectoryFileName"></param>
  56. public static void Directory_Create(string DirectoryFileName)
  57. {
  58. if (!Directory.Exists(DirectoryFileName))
  59. {
  60. Directory.CreateDirectory(DirectoryFileName);
  61. }
  62. }
  63. /// <summary>
  64. /// 创建一个新的文件
  65. /// </summary>
  66. /// <param name="strPath"></param>
  67. /// <returns></returns>
  68. public static bool CreateNewFile(string strPath)
  69. {
  70. try
  71. {
  72. if (!File.Exists(strPath))
  73. {
  74. using (FileStream fs = new FileStream(strPath, FileMode.CreateNew))
  75. fs.Close();
  76. }
  77. //if (!(File.Exists(strPath)))
  78. //{
  79. // File.Create(strPath).Close();
  80. //}
  81. return true;
  82. }
  83. catch { return false; }
  84. }
  85. /// <summary>
  86. /// 根据路径创建路径的所有文件夹
  87. /// </summary>
  88. /// <param name="folderPath"></param>
  89. /// <returns></returns>
  90. public static bool GreatNewFolder(string folderPath)
  91. {
  92. try
  93. {
  94. string[] pathArray;
  95. string allPath = string.Empty;
  96. pathArray = folderPath.Split('\\');
  97. allPath = pathArray[0];
  98. for (int i = 0; i < pathArray.Length - 1; i++)
  99. {
  100. allPath = allPath + "\\" + pathArray[i + 1];
  101. if (Directory.Exists(allPath) == false && pathArray[i + 1].IndexOf(".") == -1)
  102. {
  103. Directory.CreateDirectory(allPath);
  104. }
  105. }
  106. return true;
  107. }
  108. catch
  109. {
  110. return false;
  111. }
  112. }
  113. /// <summary>
  114. /// 判断文件是否存在
  115. /// </summary>
  116. /// <param name="path">文件路径</param>
  117. /// <returns></returns>
  118. public static bool IsFileExist(string path)
  119. {
  120. return File.Exists(path);
  121. }
  122. /// <summary>
  123. /// 删除指定文件
  124. /// </summary>
  125. /// <param name="path">文件路径</param>
  126. /// <returns></returns>
  127. public static bool DeleteFile(string path)
  128. {
  129. try
  130. {
  131. if (IsFileExist(path))
  132. {
  133. File.Delete(path);
  134. return true;
  135. }
  136. else
  137. {
  138. return false;
  139. }
  140. }
  141. catch
  142. {
  143. return false;
  144. }
  145. }
  146. /// <summary>
  147. /// 复制指定文件到目标文件
  148. /// </summary>
  149. /// <param name="sourcePath">指定文件路径</param>
  150. /// <param name="destinationPath">目标文件路径</param>
  151. /// <returns></returns>
  152. public static bool CopyFile(string sourceFileName, string destFileName,bool isCoverage)
  153. {
  154. try
  155. {
  156. if (IsFileExist(sourceFileName))
  157. {
  158. File.Copy(sourceFileName, destFileName,isCoverage);
  159. return true;
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. catch
  167. {
  168. return false;
  169. }
  170. }
  171. /// <summary>
  172. /// 剪切指定文件到指定路径 可更改文件名
  173. /// </summary>
  174. /// <param name="sourcePath">指定文件路径</param>
  175. /// <param name="destinationPath">目标文件路径</param>
  176. /// <returns></returns>
  177. public static bool MoveFile(string sourceFileName, string destFileName)
  178. {
  179. try
  180. {
  181. if (IsFileExist(sourceFileName))
  182. {
  183. File.Move(sourceFileName, destFileName);
  184. return true;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. }
  191. catch
  192. {
  193. return false;
  194. }
  195. }
  196. /// <summary>
  197. /// 复制指定文件夹到指定路径
  198. /// </summary>
  199. /// <param name="sourcePath">指定文件夹路径</param>
  200. /// <param name="destPath">目标路径</param>
  201. /// <returns></returns>
  202. public static bool CopyFolder(string sourcePath, string destPath)
  203. {
  204. string path;
  205. string subFolderPath;
  206. try
  207. {
  208. DirectoryInfo folderPath = new DirectoryInfo(sourcePath);
  209. if (folderPath.Exists)
  210. {
  211. if (!Directory.Exists(destPath))
  212. Directory.CreateDirectory(destPath);
  213. foreach (string sub in Directory.GetDirectories(sourcePath)) //获取指定目录中的子目录的名称(包括其路径)
  214. {
  215. subFolderPath = destPath + "\\" + Path.GetFileName(sub);
  216. CopyFolder(sub, subFolderPath);
  217. }
  218. foreach (string file in Directory.GetFiles(sourcePath)) //返回指定目录中文件的名称(包括其路径)
  219. {
  220. path = destPath + "\\" + Path.GetFileName(file);//返回指定路径字符串的文件名和扩展名
  221. File.Copy(file, path, true);
  222. }
  223. return true;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. catch
  231. {
  232. return false;
  233. }
  234. }
  235. #endregion
  236. #region FileDelete
  237. public string str_DeletePath = string.Empty;
  238. public int i_HowLongFile = 72;
  239. public int[] i_DeleteTime;
  240. public string str_Type = string.Empty;
  241. public void FilesAllDelete(string strDeletePath, string strType, int iHowLongFile, int[] iDeleteTime)
  242. {
  243. str_DeletePath = strDeletePath;
  244. str_Type = strType;
  245. i_HowLongFile = iHowLongFile;
  246. i_DeleteTime = iDeleteTime;
  247. Thread DeleteFileThread = new Thread(new ThreadStart(DeleteFile))
  248. {
  249. IsBackground = true
  250. };
  251. DeleteFileThread.Start();
  252. }
  253. public void DeleteFile()
  254. {
  255. int NowHour;
  256. bool IsDelete = false;
  257. while (true)
  258. {
  259. NowHour = DateTime.Now.Hour;
  260. if (i_DeleteTime.Any(a => a == NowHour) && !IsDelete)
  261. {
  262. StartDelete(str_DeletePath);
  263. IsDelete = true;
  264. }
  265. if (i_DeleteTime.Any(a => a != NowHour) && IsDelete)
  266. {
  267. IsDelete = false;
  268. }
  269. Thread.Sleep(1000);
  270. }
  271. }
  272. public bool StartDelete(string TPath)
  273. {
  274. try
  275. {
  276. ListFiles(new DirectoryInfo(TPath));
  277. KillEmptyDirectory(TPath);
  278. return true;
  279. }
  280. catch (IOException ex)
  281. {
  282. MessageBox.Show($"删除路径{str_DeletePath}文件失败\r\nFile deletion failed\r\n{ex.Message}", "Warning");
  283. return false;
  284. }
  285. }
  286. public void ListFiles(FileSystemInfo info)
  287. {
  288. if (!info.Exists) return;
  289. //不是目录
  290. if (!(info is DirectoryInfo dir)) return;
  291. FileSystemInfo[] files = dir.GetFileSystemInfos();
  292. DateTime nowtime = DateTime.Now;
  293. DateTime fileDate;
  294. TimeSpan cc;
  295. string fullpath;
  296. for (int i = 0; i < files.Length; i++)
  297. {
  298. //是文件
  299. if (files[i] is FileInfo file)
  300. {
  301. fullpath = file.FullName;
  302. //fileDate=file.CreationTime;
  303. fileDate = file.LastWriteTime;
  304. cc = nowtime - fileDate;
  305. if (cc.Days * 24 + cc.Hours > i_HowLongFile && fullpath.Contains(str_Type))
  306. {
  307. File.Delete(fullpath);
  308. }
  309. }
  310. else
  311. {
  312. ListFiles(files[i]);
  313. }
  314. }
  315. }
  316. public void KillEmptyDirectory(String storagepath)
  317. {
  318. DirectoryInfo dir = new DirectoryInfo(storagepath);
  319. DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);
  320. foreach (DirectoryInfo subdir in subdirs)
  321. {
  322. FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
  323. if (subFiles.Length == 0)
  324. {
  325. subdir.Delete();
  326. }
  327. }
  328. }
  329. #endregion
  330. }
  331. }