新野县12345_后端

DirFileHelper.cs 30KB

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