DirFile.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. namespace Ant.Service.Common
  5. {
  6. /// <summary>
  7. /// 文件操作夹
  8. /// </summary>
  9. public static class DirFile
  10. {
  11. #region 检测指定目录是否存在
  12. /// <summary>
  13. /// 检测指定目录是否存在
  14. /// </summary>
  15. /// <param name="directoryPath">目录的绝对路径</param>
  16. /// <returns></returns>
  17. public static bool IsExistDirectory(string directoryPath)
  18. {
  19. return Directory.Exists(directoryPath);
  20. }
  21. #endregion
  22. #region 检测指定文件是否存在,如果存在返回true
  23. /// <summary>
  24. /// 检测指定文件是否存在,如果存在则返回true。
  25. /// </summary>
  26. /// <param name="filePath">文件的绝对路径</param>
  27. public static bool IsExistFile(string filePath)
  28. {
  29. return File.Exists(filePath);
  30. }
  31. #endregion
  32. #region 获取指定目录中的文件列表
  33. /// <summary>
  34. /// 获取指定目录中所有文件列表
  35. /// </summary>
  36. /// <param name="directoryPath">指定目录的绝对路径</param>
  37. public static string[] GetFileNames(string directoryPath)
  38. {
  39. //如果目录不存在,则抛出异常
  40. if (!IsExistDirectory(directoryPath))
  41. {
  42. throw new FileNotFoundException();
  43. }
  44. //获取文件列表
  45. return Directory.GetFiles(directoryPath);
  46. }
  47. #endregion
  48. #region 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
  49. /// <summary>
  50. /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
  51. /// </summary>
  52. /// <param name="directoryPath">指定目录的绝对路径</param>
  53. public static string[] GetDirectories(string directoryPath)
  54. {
  55. try
  56. {
  57. return Directory.GetDirectories(directoryPath);
  58. }
  59. catch (IOException ex)
  60. {
  61. throw ex;
  62. }
  63. }
  64. #endregion
  65. #region 获取指定目录及子目录中所有文件列表
  66. /// <summary>
  67. /// 获取指定目录及子目录中所有文件列表
  68. /// </summary>
  69. /// <param name="directoryPath">指定目录的绝对路径</param>
  70. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  71. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  72. /// <param name="isSearchChild">是否搜索子目录</param>
  73. public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
  74. {
  75. //如果目录不存在,则抛出异常
  76. if (!IsExistDirectory(directoryPath))
  77. {
  78. throw new FileNotFoundException();
  79. }
  80. try
  81. {
  82. if (isSearchChild)
  83. {
  84. return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
  85. }
  86. else
  87. {
  88. return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  89. }
  90. }
  91. catch (IOException ex)
  92. {
  93. throw ex;
  94. }
  95. }
  96. #endregion
  97. #region 检测指定目录是否为空
  98. /// <summary>
  99. /// 检测指定目录是否为空
  100. /// </summary>
  101. /// <param name="directoryPath">指定目录的绝对路径</param>
  102. public static bool IsEmptyDirectory(string directoryPath)
  103. {
  104. try
  105. {
  106. //判断是否存在文件
  107. string[] fileNames = GetFileNames(directoryPath);
  108. if (fileNames.Length > 0)
  109. {
  110. return false;
  111. }
  112. //判断是否存在文件夹
  113. string[] directoryNames = GetDirectories(directoryPath);
  114. if (directoryNames.Length > 0)
  115. {
  116. return false;
  117. }
  118. return true;
  119. }
  120. catch
  121. {
  122. //这里记录日志
  123. //LogHelper.WriteTraceLog(TraceLogLevel.Error, "操作失败");
  124. return true;
  125. }
  126. }
  127. #endregion
  128. #region 检测指定目录中是否存在指定的文件
  129. /// <summary>
  130. /// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
  131. /// </summary>
  132. /// <param name="directoryPath">指定目录的绝对路径</param>
  133. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  134. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  135. public static bool Contains(string directoryPath, string searchPattern)
  136. {
  137. try
  138. {
  139. //获取指定的文件列表
  140. string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
  141. //判断指定文件是否存在
  142. if (fileNames.Length == 0)
  143. {
  144. return false;
  145. }
  146. else
  147. {
  148. return true;
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. throw new Exception(ex.Message);
  154. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  155. }
  156. }
  157. /// <summary>
  158. /// 检测指定目录中是否存在指定的文件
  159. /// </summary>
  160. /// <param name="directoryPath">指定目录的绝对路径</param>
  161. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  162. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  163. /// <param name="isSearchChild">是否搜索子目录</param>
  164. public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
  165. {
  166. try
  167. {
  168. //获取指定的文件列表
  169. string[] fileNames = GetFileNames(directoryPath, searchPattern, true);
  170. //判断指定文件是否存在
  171. if (fileNames.Length == 0)
  172. {
  173. return false;
  174. }
  175. else
  176. {
  177. return true;
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. throw new Exception(ex.Message);
  183. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  184. }
  185. }
  186. /// <summary>
  187. /// 检查某个文件是否存在
  188. /// </summary>
  189. /// <param name="filePath">文件的物理路径</param>
  190. /// <returns></returns>
  191. public static bool IsExistFiles(string filePath)
  192. {
  193. try
  194. {
  195. if (System.IO.File.Exists(filePath))
  196. return true;
  197. }
  198. catch (Exception ex) { throw new Exception(ex.Message + "\r\n" + "May be other error:文件不存在或禁止访问!"); }
  199. return false;
  200. }
  201. #endregion
  202. #region 创建目录
  203. /// <summary>
  204. /// 创建目录
  205. /// </summary>
  206. /// <param name="dir">要创建的目录路径包括目录名</param>
  207. public static void CreateDir(string dir)
  208. {
  209. if (dir.Length == 0) return;
  210. if (!Directory.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir))
  211. Directory.CreateDirectory(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir);
  212. }
  213. #endregion
  214. #region 删除目录
  215. /// <summary>
  216. /// 删除目录
  217. /// </summary>
  218. /// <param name="dir">要删除的目录路径和名称</param>
  219. public static void DeleteDir(string dir)
  220. {
  221. if (dir.Length == 0) return;
  222. if (Directory.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir))
  223. Directory.Delete(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir);
  224. }
  225. #endregion
  226. #region 删除文件
  227. /// <summary>
  228. /// 删除文件
  229. /// </summary>
  230. /// <param name="file">要删除的文件路径和名称</param>
  231. public static bool DeleteGivenFile(string file)
  232. {
  233. if (File.Exists(file))
  234. {
  235. File.Delete(file);
  236. return true;
  237. }
  238. return false;
  239. }
  240. /// <summary>
  241. /// 删除文件
  242. /// </summary>
  243. /// <param name="file">要删除的文件路径和名称</param>
  244. public static void DeleteFile(string file)
  245. {
  246. if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + file))
  247. File.Delete(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + file);
  248. }
  249. #endregion
  250. #region 创建文件
  251. /// <summary>
  252. /// 创建文件
  253. /// </summary>
  254. /// <param name="dir">带后缀的文件名</param>
  255. /// <param name="pagestr">文件内容</param>
  256. public static void CreateFile(string dir, string pagestr)
  257. {
  258. dir = dir.Replace("/", "\\");
  259. if (dir.IndexOf("\\") > -1)
  260. CreateDir(dir.Substring(0, dir.LastIndexOf("\\")));
  261. System.IO.StreamWriter sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir, false, System.Text.Encoding.GetEncoding("GB2312"));
  262. sw.Write(pagestr);
  263. sw.Close();
  264. }
  265. #endregion
  266. #region 移动文件(剪贴--粘贴)
  267. /// <summary>
  268. /// 移动文件(剪贴--粘贴)
  269. /// </summary>
  270. /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param>
  271. /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param>
  272. public static void MoveFile(string dir1, string dir2)
  273. {
  274. dir1 = dir1.Replace("/", "\\");
  275. dir2 = dir2.Replace("/", "\\");
  276. if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1))
  277. File.Move(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1, System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir2);
  278. }
  279. #endregion
  280. #region 复制文件
  281. /// <summary>
  282. /// 复制文件
  283. /// </summary>
  284. /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param>
  285. /// <param name="dir2">目标位置,并指定新的文件名</param>
  286. public static void CopyFile(string dir1, string dir2)
  287. {
  288. dir1 = dir1.Replace("/", "\\");
  289. dir2 = dir2.Replace("/", "\\");
  290. if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1))
  291. {
  292. File.Copy(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir1, System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir2, true);
  293. }
  294. }
  295. #endregion
  296. #region 根据时间得到目录名 / 格式:yyyyMMdd 或者 HHmmssff
  297. /// <summary>
  298. /// 根据时间得到目录名yyyyMMdd
  299. /// </summary>
  300. /// <returns></returns>
  301. public static string GetDateDir()
  302. {
  303. return DateTime.Now.ToString("yyyyMMdd");
  304. }
  305. /// <summary>
  306. /// 根据时间得到文件名HHmmssff
  307. /// </summary>
  308. /// <returns></returns>
  309. public static string GetDateFile()
  310. {
  311. return DateTime.Now.ToString("HHmmssff");
  312. }
  313. #endregion
  314. #region 复制文件夹
  315. /// <summary>
  316. /// 复制文件夹(递归)
  317. /// </summary>
  318. /// <param name="varFromDirectory">源文件夹路径</param>
  319. /// <param name="varToDirectory">目标文件夹路径</param>
  320. public static void CopyFolder(string varFromDirectory, string varToDirectory)
  321. {
  322. Directory.CreateDirectory(varToDirectory);
  323. if (!Directory.Exists(varFromDirectory)) return;
  324. string[] directories = Directory.GetDirectories(varFromDirectory);
  325. if (directories.Length > 0)
  326. {
  327. foreach (string d in directories)
  328. {
  329. CopyFolder(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
  330. }
  331. }
  332. string[] files = Directory.GetFiles(varFromDirectory);
  333. if (files.Length > 0)
  334. {
  335. foreach (string s in files)
  336. {
  337. File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\")), true);
  338. }
  339. }
  340. }
  341. #endregion
  342. #region 检查文件,如果文件不存在则创建
  343. /// <summary>
  344. /// 检查文件,如果文件不存在则创建
  345. /// </summary>
  346. /// <param name="FilePath">路径,包括文件名</param>
  347. public static void ExistsFile(string FilePath)
  348. {
  349. //if(!File.Exists(FilePath))
  350. //File.Create(FilePath);
  351. //以上写法会报错,详细解释请看下文.........
  352. if (!File.Exists(FilePath))
  353. {
  354. FileStream fs = File.Create(FilePath);
  355. fs.Close();
  356. }
  357. }
  358. #endregion
  359. #region 删除指定文件夹对应其他文件夹里的文件
  360. /// <summary>
  361. /// 删除指定文件夹对应其他文件夹里的文件
  362. /// </summary>
  363. /// <param name="varFromDirectory">指定文件夹路径</param>
  364. /// <param name="varToDirectory">对应其他文件夹路径</param>
  365. public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory)
  366. {
  367. Directory.CreateDirectory(varToDirectory);
  368. if (!Directory.Exists(varFromDirectory)) return;
  369. string[] directories = Directory.GetDirectories(varFromDirectory);
  370. if (directories.Length > 0)
  371. {
  372. foreach (string d in directories)
  373. {
  374. DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
  375. }
  376. }
  377. string[] files = Directory.GetFiles(varFromDirectory);
  378. if (files.Length > 0)
  379. {
  380. foreach (string s in files)
  381. {
  382. File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\\")));
  383. }
  384. }
  385. }
  386. #endregion
  387. #region 从文件的绝对路径中获取文件名( 包含扩展名 )
  388. /// <summary>
  389. /// 从文件的绝对路径中获取文件名( 包含扩展名 )
  390. /// </summary>
  391. /// <param name="filePath">文件的绝对路径</param>
  392. public static string GetFileName(string filePath)
  393. {
  394. //获取文件的名称
  395. FileInfo fi = new FileInfo(filePath);
  396. return fi.Name;
  397. }
  398. #endregion
  399. #region 创建一个目录
  400. /// <summary>
  401. /// 创建一个目录
  402. /// </summary>
  403. /// <param name="directoryPath">目录的绝对路径</param>
  404. public static void CreateDirectory(string directoryPath)
  405. {
  406. //如果目录不存在则创建该目录
  407. if (!IsExistDirectory(directoryPath))
  408. {
  409. Directory.CreateDirectory(directoryPath);
  410. }
  411. }
  412. #endregion
  413. #region 创建一个文件
  414. /// <summary>
  415. /// 创建一个文件。
  416. /// </summary>
  417. /// <param name="filePath">文件的绝对路径</param>
  418. public static void CreateFile(string filePath)
  419. {
  420. try
  421. {
  422. //如果文件不存在则创建该文件
  423. if (!IsExistFile(filePath))
  424. {
  425. //创建一个FileInfo对象
  426. FileInfo file = new FileInfo(filePath);
  427. //创建文件
  428. FileStream fs = file.Create();
  429. //关闭文件流
  430. fs.Close();
  431. }
  432. }
  433. catch (Exception ex)
  434. {
  435. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  436. throw ex;
  437. }
  438. }
  439. /// <summary>
  440. /// 创建一个文件,并将字节流写入文件。
  441. /// </summary>
  442. /// <param name="filePath">文件的绝对路径</param>
  443. /// <param name="buffer">二进制流数据</param>
  444. public static void CreateFile(string filePath, byte[] buffer)
  445. {
  446. try
  447. {
  448. //如果文件不存在则创建该文件
  449. if (!IsExistFile(filePath))
  450. {
  451. //创建一个FileInfo对象
  452. FileInfo file = new FileInfo(filePath);
  453. //创建文件
  454. FileStream fs = file.Create();
  455. //写入二进制流
  456. fs.Write(buffer, 0, buffer.Length);
  457. //关闭文件流
  458. fs.Close();
  459. }
  460. }
  461. catch (Exception ex)
  462. {
  463. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  464. throw ex;
  465. }
  466. }
  467. #endregion
  468. #region 获取文本文件的行数
  469. /// <summary>
  470. /// 获取文本文件的行数
  471. /// </summary>
  472. /// <param name="filePath">文件的绝对路径</param>
  473. public static int GetLineCount(string filePath)
  474. {
  475. //将文本文件的各行读到一个字符串数组中
  476. string[] rows = File.ReadAllLines(filePath);
  477. //返回行数
  478. return rows.Length;
  479. }
  480. #endregion
  481. #region 获取一个文件的长度
  482. /// <summary>
  483. /// 获取一个文件的长度,单位为Byte
  484. /// </summary>
  485. /// <param name="filePath">文件的绝对路径</param>
  486. public static int GetFileSize(string filePath)
  487. {
  488. //创建一个文件对象
  489. FileInfo fi = new FileInfo(filePath);
  490. //获取文件的大小
  491. return (int)fi.Length;
  492. }
  493. #endregion
  494. #region 获取指定目录中的子目录列表
  495. /// <summary>
  496. /// 获取指定目录及子目录中所有子目录列表
  497. /// </summary>
  498. /// <param name="directoryPath">指定目录的绝对路径</param>
  499. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  500. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  501. /// <param name="isSearchChild">是否搜索子目录</param>
  502. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  503. {
  504. try
  505. {
  506. if (isSearchChild)
  507. {
  508. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
  509. }
  510. else
  511. {
  512. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  513. }
  514. }
  515. catch (IOException ex)
  516. {
  517. throw ex;
  518. }
  519. }
  520. #endregion
  521. #region 向文本文件写入内容
  522. /// <summary>
  523. /// 向文本文件中写入内容
  524. /// </summary>
  525. /// <param name="filePath">文件的绝对路径</param>
  526. /// <param name="text">写入的内容</param>
  527. /// <param name="encoding">编码</param>
  528. public static void WriteText(string filePath, string text, Encoding encoding)
  529. {
  530. //向文件写入内容
  531. File.WriteAllText(filePath, text, encoding);
  532. }
  533. #endregion
  534. #region 向文本文件的尾部追加内容
  535. /// <summary>
  536. /// 向文本文件的尾部追加内容
  537. /// </summary>
  538. /// <param name="filePath">文件的绝对路径</param>
  539. /// <param name="content">写入的内容</param>
  540. public static void AppendText(string filePath, string content)
  541. {
  542. File.AppendAllText(filePath, content);
  543. }
  544. #endregion
  545. #region 将现有文件的内容复制到新文件中
  546. /// <summary>
  547. /// 将源文件的内容复制到目标文件中
  548. /// </summary>
  549. /// <param name="sourceFilePath">源文件的绝对路径</param>
  550. /// <param name="destFilePath">目标文件的绝对路径</param>
  551. public static void Copy(string sourceFilePath, string destFilePath)
  552. {
  553. File.Copy(sourceFilePath, destFilePath, true);
  554. }
  555. #endregion
  556. #region 将文件移动到指定目录
  557. /// <summary>
  558. /// 将文件移动到指定目录
  559. /// </summary>
  560. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  561. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  562. public static void Move(string sourceFilePath, string descDirectoryPath)
  563. {
  564. //获取源文件的名称
  565. string sourceFileName = GetFileName(sourceFilePath);
  566. if (IsExistDirectory(descDirectoryPath))
  567. {
  568. //如果目标中存在同名文件,则删除
  569. if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
  570. {
  571. DeleteFile(descDirectoryPath + "\\" + sourceFileName);
  572. }
  573. //将文件移动到指定目录
  574. File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
  575. }
  576. }
  577. #endregion
  578. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  579. /// <summary>
  580. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  581. /// </summary>
  582. /// <param name="filePath">文件的绝对路径</param>
  583. public static string GetFileNameNoExtension(string filePath)
  584. {
  585. //获取文件的名称
  586. FileInfo fi = new FileInfo(filePath);
  587. return fi.Name.Split('.')[0];
  588. }
  589. #endregion
  590. #region 从文件的绝对路径中获取扩展名
  591. /// <summary>
  592. /// 从文件的绝对路径中获取扩展名
  593. /// </summary>
  594. /// <param name="filePath">文件的绝对路径</param>
  595. public static string GetExtension(string filePath)
  596. {
  597. //获取文件的名称
  598. FileInfo fi = new FileInfo(filePath);
  599. return fi.Extension;
  600. }
  601. #endregion
  602. #region 清空指定目录
  603. /// <summary>
  604. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  605. /// </summary>
  606. /// <param name="directoryPath">指定目录的绝对路径</param>
  607. public static void ClearDirectory(string directoryPath)
  608. {
  609. if (IsExistDirectory(directoryPath))
  610. {
  611. //删除目录中所有的文件
  612. string[] fileNames = GetFileNames(directoryPath);
  613. for (int i = 0; i < fileNames.Length; i++)
  614. {
  615. DeleteFile(fileNames[i]);
  616. }
  617. //删除目录中所有的子目录
  618. string[] directoryNames = GetDirectories(directoryPath);
  619. for (int i = 0; i < directoryNames.Length; i++)
  620. {
  621. DeleteDirectory(directoryNames[i]);
  622. }
  623. }
  624. }
  625. #endregion
  626. #region 清空文件内容
  627. /// <summary>
  628. /// 清空文件内容
  629. /// </summary>
  630. /// <param name="filePath">文件的绝对路径</param>
  631. public static void ClearFile(string filePath)
  632. {
  633. //删除文件
  634. File.Delete(filePath);
  635. //重新创建该文件
  636. CreateFile(filePath);
  637. }
  638. #endregion
  639. #region 删除指定目录
  640. /// <summary>
  641. /// 删除指定目录及其所有子目录
  642. /// </summary>
  643. /// <param name="directoryPath">指定目录的绝对路径</param>
  644. public static void DeleteDirectory(string directoryPath)
  645. {
  646. if (IsExistDirectory(directoryPath))
  647. {
  648. Directory.Delete(directoryPath, true);
  649. }
  650. }
  651. #endregion
  652. }
  653. }