星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileToolsCommon.cs 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Forms;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// 文件工具公共方法
  11. /// 创建人:赵耀
  12. /// 创建时间:2018年11月6日
  13. /// </summary>
  14. public static class FileToolsCommon
  15. {
  16. #region 获取文件的绝对路径
  17. /// <summary>
  18. /// 获取文件的绝对路径
  19. /// </summary>
  20. /// <param name="Path">相对路径</param>
  21. /// <returns></returns>
  22. public static string GetFileAbsolutePath(string Path = "")
  23. {
  24. try
  25. {
  26. if (!string.IsNullOrWhiteSpace(Path))
  27. {
  28. Path = Path.Replace("\\", "/");
  29. if (Path != "/")
  30. {
  31. if (Path.Substring(1, 1) == ":")
  32. return Path;
  33. if (Path.Substring(0, 1) != "/")
  34. Path = "/" + Path;
  35. }
  36. }
  37. string AbsolutePath = Application.StartupPath.ToString().Replace("\\", "/") + Path;
  38. return AbsolutePath;
  39. }
  40. catch (Exception)
  41. {
  42. return Path;
  43. }
  44. }
  45. /// <summary>
  46. /// 获取合法路径 结尾带/
  47. /// </summary>
  48. /// <returns></returns>
  49. public static string GetLegalPath(string FilePath)
  50. {
  51. FilePath = System.IO.Path.Combine(FilePath.Replace("\\", "/"));
  52. FilePath += (FilePath.Substring(FilePath.Length - 1, 1) == "/" ? "" : "/");
  53. return FilePath;
  54. }
  55. #endregion
  56. #region 检测路径是否合法
  57. /// <summary>
  58. /// 检测路径是否合法
  59. /// </summary>
  60. /// <param name="FilePathName">绝对路径</param>
  61. /// <param name="ErrMessage">返回错误</param>
  62. /// <returns></returns>
  63. public static bool IsLegalPath(string FilePathName, out string ErrMessage)
  64. {
  65. string FilePath;
  66. string FileName;
  67. ErrMessage = "";
  68. try
  69. {
  70. FilePathName = GetLegalPath(FilePathName);
  71. FilePath = GetDirectoryName(FilePathName);
  72. FileName = GetIOFileName(FilePathName);
  73. }
  74. catch (Exception)
  75. {
  76. ErrMessage = "路径或文件名不合法!";
  77. return false;
  78. }
  79. Regex regex = new Regex(@"^([a-zA-Z]:\\)?[^\/\:\*\?\""\<\>\|\,]*$");
  80. Match m = regex.Match(FilePath);
  81. if (!m.Success)
  82. {
  83. ErrMessage = "非法的文件保存路径!";
  84. return false;
  85. }
  86. if (!string.IsNullOrWhiteSpace(FileName))
  87. {
  88. regex = new Regex(@"^[^\/\:\*\?\""\<\>\|\,]+$");
  89. m = regex.Match(FileName);
  90. if (!m.Success)
  91. {
  92. ErrMessage = "非法的文件名!文件名中包含\\ / : * ? \" < > |等字符!";
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. #endregion
  99. #region 检测指定目录是否存在
  100. /// <summary>
  101. /// 检测指定目录是否存在
  102. /// </summary>
  103. /// <param name="directoryPath">目录的绝对路径</param>
  104. public static bool IsExistDirectory(string directoryPath)
  105. {
  106. return Directory.Exists(directoryPath);
  107. }
  108. #endregion
  109. #region 检测指定文件是否存在
  110. /// <summary>
  111. /// 检测指定文件是否存在,如果存在则返回true。
  112. /// </summary>
  113. /// <param name="filePath">文件的绝对路径</param>
  114. public static bool IsExistFile(string filePath)
  115. {
  116. return File.Exists(filePath);
  117. }
  118. #endregion
  119. #region 检测指定目录是否为空
  120. /// <summary>
  121. /// 检测指定目录是否为空
  122. /// </summary>
  123. /// <param name="directoryPath">指定目录的绝对路径</param>
  124. public static bool IsEmptyDirectory(string directoryPath)
  125. {
  126. try
  127. {
  128. //判断是否存在文件
  129. string[] fileNames = GetFileNames(directoryPath);
  130. if (fileNames.Length > 0)
  131. {
  132. return false;
  133. }
  134. //判断是否存在文件夹
  135. string[] directoryNames = GetDirectories(directoryPath);
  136. return directoryNames.Length <= 0;
  137. }
  138. catch
  139. {
  140. return false;
  141. }
  142. }
  143. #endregion
  144. #region 检测指定目录中是否存在指定的文件
  145. /// <summary>
  146. /// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
  147. /// </summary>
  148. /// <param name="directoryPath">指定目录的绝对路径</param>
  149. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  150. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  151. public static bool Contains(string directoryPath, string searchPattern)
  152. {
  153. try
  154. {
  155. //获取指定的文件列表
  156. string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
  157. //判断指定文件是否存在
  158. return fileNames.Length != 0;
  159. }
  160. catch
  161. {
  162. return false;
  163. }
  164. }
  165. /// <summary>
  166. /// 检测指定目录中是否存在指定的文件
  167. /// </summary>
  168. /// <param name="directoryPath">指定目录的绝对路径</param>
  169. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  170. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  171. /// <param name="isSearchChild">是否搜索子目录</param>
  172. public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
  173. {
  174. try
  175. {
  176. //获取指定的文件列表
  177. string[] fileNames = GetFileNames(directoryPath, searchPattern, isSearchChild);
  178. //判断指定文件是否存在
  179. return fileNames.Length != 0;
  180. }
  181. catch
  182. {
  183. return false;
  184. }
  185. }
  186. #endregion
  187. #region 创建一个目录
  188. /// <summary>
  189. /// 创建一个目录
  190. /// </summary>
  191. /// <param name="directoryPath">目录的绝对路径</param>
  192. public static void CreateDirectory(string directoryPath)
  193. {
  194. try
  195. {
  196. //如果目录不存在则创建该目录
  197. if (!IsExistDirectory(directoryPath))
  198. {
  199. Directory.CreateDirectory(directoryPath);
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. throw new ApplicationException(ex.Message); //"请使用管理员权限运行!"
  205. }
  206. }
  207. #endregion
  208. #region 创建一个文件
  209. /// <summary>
  210. /// 创建一个文件。
  211. /// </summary>
  212. /// <param name="filePath">文件的绝对路径</param>
  213. public static bool CreateFile(string filePath)
  214. {
  215. try
  216. {
  217. //如果文件不存在则创建该文件
  218. if (!IsExistFile(filePath))
  219. {
  220. //创建一个FileInfo对象
  221. FileInfo file = new FileInfo(filePath);
  222. //创建文件
  223. FileStream fs = file.Create();
  224. //关闭文件流
  225. fs.Close();
  226. }
  227. }
  228. catch
  229. {
  230. return false;
  231. }
  232. return true;
  233. }
  234. /// <summary>
  235. /// 创建一个文件,并将字节流写入文件。
  236. /// </summary>
  237. /// <param name="filePath">文件的绝对路径</param>
  238. /// <param name="buffer">二进制流数据</param>
  239. public static bool CreateFile(string filePath, byte[] buffer)
  240. {
  241. try
  242. {
  243. //如果文件不存在则创建该文件
  244. if (!IsExistFile(filePath))
  245. {
  246. //创建一个FileInfo对象
  247. FileInfo file = new FileInfo(filePath);
  248. //创建文件
  249. FileStream fs = file.Create();
  250. //写入二进制流
  251. fs.Write(buffer, 0, buffer.Length);
  252. //关闭文件流
  253. fs.Close();
  254. }
  255. }
  256. catch
  257. {
  258. return false;
  259. }
  260. return true;
  261. }
  262. #endregion
  263. #region 获取文本文件的行数
  264. /// <summary>
  265. /// 获取文本文件的行数
  266. /// </summary>
  267. /// <param name="filePath">文件的绝对路径</param>
  268. public static int GetLineCount(string filePath)
  269. {
  270. //将文本文件的各行读到一个字符串数组中
  271. string[] rows = File.ReadAllLines(filePath);
  272. //返回行数
  273. return rows.Length;
  274. }
  275. #endregion
  276. #region 获取一个文件的长度
  277. /// <summary>
  278. /// 获取一个文件的长度,单位为Byte
  279. /// </summary>
  280. /// <param name="filePath">文件的绝对路径</param>
  281. public static int GetFileSize(string filePath)
  282. {
  283. //创建一个文件对象
  284. FileInfo fi = new FileInfo(filePath);
  285. //获取文件的大小
  286. return (int)fi.Length;
  287. }
  288. /// <summary>
  289. /// 获取一个文件的长度,单位为KB
  290. /// </summary>
  291. /// <param name="filePath">文件的路径</param>
  292. public static double GetFileSizeByKB(string filePath)
  293. {
  294. //创建一个文件对象
  295. FileInfo fi = new FileInfo(filePath);
  296. long size = fi.Length / 1024;
  297. //获取文件的大小
  298. return double.Parse(size.ToString());
  299. }
  300. /// <summary>
  301. /// 获取一个文件的长度,单位为MB
  302. /// </summary>
  303. /// <param name="filePath">文件的路径</param>
  304. public static double GetFileSizeByMB(string filePath)
  305. {
  306. //创建一个文件对象
  307. FileInfo fi = new FileInfo(filePath);
  308. long size = fi.Length / 1024 / 1024;
  309. //获取文件的大小
  310. return double.Parse(size.ToString());
  311. }
  312. #endregion
  313. #region 获取指定目录中的文件列表
  314. /// <summary>
  315. /// 获取指定目录中所有文件列表
  316. /// </summary>
  317. /// <param name="directoryPath">指定目录的绝对路径</param>
  318. public static FileInfo[] GetFileInfos(string directoryPath, string files = null)
  319. {
  320. //如果目录不存在,则抛出异常
  321. if (!IsExistDirectory(directoryPath))
  322. {
  323. return null;//throw new FileNotFoundException();
  324. }
  325. DirectoryInfo folder = new DirectoryInfo(directoryPath);
  326. FileInfo[] fileList;
  327. if (files == null)
  328. fileList = folder.GetFiles();
  329. else
  330. fileList = folder.GetFiles(files);
  331. //获取文件列表
  332. return fileList;
  333. }
  334. /// <summary>
  335. /// 获取指定目录中所有文件列表
  336. /// </summary>
  337. /// <param name="directoryPath">指定目录的绝对路径</param>
  338. public static string[] GetFileNames(string directoryPath)
  339. {
  340. //如果目录不存在,则抛出异常
  341. if (!IsExistDirectory(directoryPath))
  342. {
  343. return null;//throw new FileNotFoundException();
  344. }
  345. //获取文件列表
  346. return Directory.GetFiles(directoryPath);
  347. }
  348. /// <summary>
  349. /// 获取指定目录及子目录中所有文件列表
  350. /// </summary>
  351. /// <param name="directoryPath">指定目录的绝对路径</param>
  352. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  353. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  354. /// <param name="isSearchChild">是否搜索子目录</param>
  355. public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
  356. {
  357. //如果目录不存在,则抛出异常
  358. if (!IsExistDirectory(directoryPath))
  359. {
  360. return null;//throw new FileNotFoundException();
  361. }
  362. try
  363. {
  364. return Directory.GetFiles(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  365. }
  366. catch
  367. {
  368. return null;
  369. }
  370. }
  371. #endregion
  372. #region 获取指定目录中的子目录列表
  373. /// <summary>
  374. /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
  375. /// </summary>
  376. /// <param name="directoryPath">指定目录的绝对路径</param>
  377. public static string[] GetDirectories(string directoryPath)
  378. {
  379. try
  380. {
  381. return Directory.GetDirectories(directoryPath);
  382. }
  383. catch
  384. {
  385. return null;
  386. }
  387. }
  388. /// <summary>
  389. /// 获取指定目录及子目录中所有子目录列表
  390. /// </summary>
  391. /// <param name="directoryPath">指定目录的绝对路径</param>
  392. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  393. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  394. /// <param name="isSearchChild">是否搜索子目录</param>
  395. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  396. {
  397. try
  398. {
  399. return Directory.GetDirectories(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  400. }
  401. catch
  402. {
  403. return null;//throw
  404. }
  405. }
  406. #endregion
  407. #region 向文本文件写入内容
  408. /// <summary>
  409. /// 向文本文件中写入内容
  410. /// </summary>
  411. /// <param name="filePath">文件的绝对路径</param>
  412. /// <param name="content">写入的内容</param>
  413. public static void WriteText(string filePath, string content)
  414. {
  415. //向文件写入内容
  416. File.WriteAllText(filePath, content);
  417. }
  418. #endregion
  419. #region 向文本文件的尾部追加内容
  420. /// <summary>
  421. /// 向文本文件的尾部追加内容
  422. /// </summary>
  423. /// <param name="filePath">文件的绝对路径</param>
  424. /// <param name="content">写入的内容</param>
  425. public static void AppendText(string filePath, string content)
  426. {
  427. File.AppendAllText(filePath, content);
  428. }
  429. #endregion
  430. #region 将现有文件的内容复制到新文件中
  431. /// <summary>
  432. /// 将源文件的内容复制到目标文件中
  433. /// </summary>
  434. /// <param name="sourceFilePath">源文件的绝对路径</param>
  435. /// <param name="destFilePath">目标文件的绝对路径</param>
  436. public static void Copy(string sourceFilePath, string destFilePath)
  437. {
  438. File.Copy(sourceFilePath, destFilePath, true);
  439. }
  440. #endregion
  441. #region 将文件移动到指定目录
  442. /// <summary>
  443. /// 将文件移动到指定目录
  444. /// </summary>
  445. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  446. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  447. public static void Move(string sourceFilePath, string descDirectoryPath)
  448. {
  449. //获取源文件的名称
  450. string sourceFileName = GetFileName(sourceFilePath);
  451. if (IsExistDirectory(descDirectoryPath))
  452. {
  453. //如果目标中存在同名文件,则删除
  454. if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
  455. {
  456. DeleteFile(descDirectoryPath + "\\" + sourceFileName);
  457. }
  458. //将文件移动到指定目录
  459. File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
  460. }
  461. }
  462. #endregion
  463. #region 将流读取到缓冲区中
  464. /// <summary>
  465. /// 将流读取到缓冲区中
  466. /// </summary>
  467. /// <param name="stream">原始流</param>
  468. public static byte[] StreamToBytes(Stream stream)
  469. {
  470. try
  471. {
  472. //创建缓冲区
  473. byte[] buffer = new byte[stream.Length];
  474. //读取流
  475. stream.Read(buffer, 0, int.Parse(stream.Length.ToString()));
  476. //返回流
  477. return buffer;
  478. }
  479. catch
  480. {
  481. return null;
  482. }
  483. finally
  484. {
  485. //关闭流
  486. stream.Close();
  487. }
  488. }
  489. #endregion
  490. #region 将文件读取到缓冲区中
  491. /// <summary>
  492. /// 将文件读取到缓冲区中
  493. /// </summary>
  494. /// <param name="filePath">文件的绝对路径</param>
  495. public static byte[] FileToBytes(string filePath)
  496. {
  497. //获取文件的大小
  498. int fileSize = GetFileSize(filePath);
  499. //创建一个临时缓冲区
  500. byte[] buffer = new byte[fileSize];
  501. //创建一个文件流
  502. FileInfo fi = new FileInfo(filePath);
  503. FileStream fs = fi.Open(FileMode.Open);
  504. try
  505. {
  506. //将文件流读入缓冲区
  507. fs.Read(buffer, 0, fileSize);
  508. return buffer;
  509. }
  510. catch
  511. {
  512. return null;
  513. }
  514. finally
  515. {
  516. //关闭文件流
  517. fs.Close();
  518. }
  519. }
  520. /// <summary>
  521. /// 读取大文件用,读取文件前面指定长度字节数
  522. /// </summary>
  523. /// <param name="filePath">文件路径</param>
  524. /// <param name="Length">读取长度,单位字节</param>
  525. /// <returns></returns>
  526. public static byte[] ReadBigFile(string filePath, int Length)
  527. {
  528. FileStream stream = new FileStream(filePath, FileMode.Open);
  529. byte[] buffer = new byte[Length];
  530. //stream.Position = startIndex;
  531. stream.Read(buffer, 0, Length);
  532. stream.Close();
  533. stream.Dispose();
  534. return buffer;
  535. }
  536. /// <summary>
  537. /// 读取指定长度的流
  538. /// </summary>
  539. /// <param name="filePath">文件位置</param>
  540. /// <param name="startIndex">开始位置</param>
  541. /// <param name="Length">长度</param>
  542. /// <returns></returns>
  543. public static byte[] ReadBigFileSpecifyLength(string filePath, long startIndex, int Length)
  544. {
  545. FileStream stream = new FileStream(filePath, FileMode.Open);
  546. if (startIndex + Length + 1 > stream.Length)
  547. {
  548. Length = (int)(stream.Length - startIndex);
  549. }
  550. byte[] buffer = new byte[Length];
  551. stream.Position = startIndex;
  552. stream.Read(buffer, 0, Length);
  553. stream.Close();
  554. stream.Dispose();
  555. return buffer;
  556. }
  557. #endregion
  558. #region 将文件读取到字符串中
  559. /// <summary>
  560. /// 将文件读取到字符串中
  561. /// </summary>
  562. /// <param name="filePath">文件的绝对路径</param>
  563. public static string FileToString(string filePath)
  564. {
  565. //return FileToString(filePath, Encoding.Default);
  566. return FileToString(filePath, Encoding.UTF8);
  567. }
  568. /// <summary>
  569. /// 将文件读取到字符串中
  570. /// </summary>
  571. /// <param name="filePath">文件的绝对路径</param>
  572. /// <param name="encoding">字符编码</param>
  573. public static string FileToString(string filePath, Encoding encoding)
  574. {
  575. //创建流读取器
  576. StreamReader reader = new StreamReader(filePath, encoding);
  577. try
  578. {
  579. //读取流
  580. return reader.ReadToEnd();
  581. }
  582. catch
  583. {
  584. return string.Empty;
  585. }
  586. finally
  587. {
  588. //关闭流读取器
  589. reader.Close();
  590. }
  591. }
  592. /// <summary>
  593. /// 读取大文件用,读取文件前面指定长度字节的字符串
  594. /// </summary>
  595. /// <param name="filePath">文件路径</param>
  596. /// <param name="Length">读取长度,单位字节</param>
  597. /// <returns></returns>
  598. public static string ReadBigFileStr(string filePath, int Length)
  599. {
  600. byte[] buffer =ReadBigFile(filePath, Length);
  601. return Encoding.Default.GetString(buffer);
  602. }
  603. #endregion
  604. #region 从文件的绝对路径中获取文件名( 包含扩展名 )
  605. /// <summary>
  606. /// 从文件的绝对路径中获取文件名( 包含扩展名 )
  607. /// </summary>
  608. /// <param name="filePath">文件的绝对路径</param>
  609. public static string GetFileName(string filePath)
  610. {
  611. //获取文件的名称
  612. FileInfo fi = new FileInfo(filePath);
  613. return fi.Name;
  614. }
  615. /// <summary>
  616. /// 使用IO从文件的绝对路径中获取文件名( 包含扩展名 )
  617. /// </summary>
  618. /// <param name="filePath">文件的绝对路径</param>
  619. public static string GetIOFileName(string filePath)
  620. {
  621. return Path.GetFileName(filePath);
  622. }
  623. #endregion
  624. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  625. /// <summary>
  626. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  627. /// </summary>
  628. /// <param name="filePath">文件的绝对路径</param>
  629. public static string GetFileNameNoExtension(string filePath)
  630. {
  631. //获取文件的名称
  632. FileInfo fi = new FileInfo(filePath);
  633. return fi.Name.Split('.')[0];
  634. }
  635. /// <summary>
  636. /// 使用IO从文件的绝对路径中获取文件名( 不包含扩展名 )
  637. /// </summary>
  638. /// <param name="filePath">文件的绝对路径</param>
  639. public static string GetIOFileNameNoExtension(string filePath)
  640. {
  641. return System.IO.Path.GetFileNameWithoutExtension(filePath);
  642. }
  643. #endregion
  644. #region 从文件的绝对路径中获取扩展名
  645. /// <summary>
  646. /// 从文件的绝对路径中获取扩展名
  647. /// </summary>
  648. /// <param name="filePath">文件的绝对路径</param>
  649. public static string GetExtension(string filePath)
  650. {
  651. //获取文件的名称
  652. FileInfo fi = new FileInfo(filePath);
  653. return fi.Extension;
  654. }
  655. /// <summary>
  656. /// 使用IO从文件的绝对路径中获取扩展名
  657. /// </summary>
  658. /// <param name="filePath">文件的绝对路径</param>
  659. public static string GetIOExtension(string filePath)
  660. {
  661. string Extension = System.IO.Path.GetExtension(filePath);
  662. return Extension;
  663. }
  664. #endregion
  665. #region 从文件的绝对路径中获取路径名
  666. /// <summary>
  667. /// 使用IO从文件的绝对路径中获取路径名
  668. /// </summary>
  669. /// <param name="filePath">文件的绝对路径</param>
  670. public static string GetDirectoryName(string filePath)
  671. {
  672. string DirectoryName = (System.IO.Path.GetDirectoryName(filePath) + @"/").Replace("\\", "/");
  673. return DirectoryName;
  674. }
  675. #endregion
  676. #region 清空指定目录
  677. /// <summary>
  678. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  679. /// </summary>
  680. /// <param name="directoryPath">指定目录的绝对路径</param>
  681. public static void ClearDirectory(string directoryPath)
  682. {
  683. if (IsExistDirectory(directoryPath))
  684. {
  685. //删除目录中所有的文件
  686. string[] fileNames = GetFileNames(directoryPath);
  687. foreach (string t in fileNames)
  688. {
  689. DeleteFile(t);
  690. }
  691. //删除目录中所有的子目录
  692. string[] directoryNames = GetDirectories(directoryPath);
  693. foreach (string t in directoryNames)
  694. {
  695. DeleteDirectory(t);
  696. }
  697. }
  698. }
  699. #endregion
  700. #region 清空文件内容
  701. /// <summary>
  702. /// 清空文件内容
  703. /// </summary>
  704. /// <param name="filePath">文件的绝对路径</param>
  705. public static void ClearFile(string filePath)
  706. {
  707. //删除文件
  708. File.Delete(filePath);
  709. //重新创建该文件
  710. CreateFile(filePath);
  711. }
  712. #endregion
  713. #region 删除指定文件
  714. /// <summary>
  715. /// 删除指定文件
  716. /// </summary>
  717. /// <param name="filePath">文件的绝对路径</param>
  718. public static void DeleteFile(string filePath)
  719. {
  720. if (IsExistFile(filePath))
  721. {
  722. File.Delete(filePath);
  723. }
  724. }
  725. #endregion
  726. #region 删除指定目录
  727. /// <summary>
  728. /// 删除指定目录及其所有子目录
  729. /// </summary>
  730. /// <param name="directoryPath">指定目录的绝对路径</param>
  731. public static void DeleteDirectory(string directoryPath)
  732. {
  733. if (IsExistDirectory(directoryPath))
  734. {
  735. Directory.Delete(directoryPath, true);
  736. }
  737. }
  738. #endregion
  739. #region 获取指定目录大小
  740. /// <summary>
  741. /// 获取目录大小
  742. /// </summary>
  743. /// <param name="directoryPath">目录地址</param>
  744. /// <returns> 单位:MB</returns>
  745. public static double PathSize(string directoryPath)
  746. {
  747. DirectoryInfo folder = new DirectoryInfo(directoryPath);
  748. double DirectorySize = DirSize(folder);
  749. for (int i = 0; i < 2; i++)
  750. {
  751. DirectorySize /= (double)1024;
  752. }
  753. return DirectorySize;
  754. }
  755. /// <summary>
  756. /// 获取目录大小
  757. /// </summary>
  758. /// <param name="d">目录</param>
  759. /// <returns>字节</returns>
  760. public static long DirSize(DirectoryInfo d)
  761. {
  762. long Size = 0;
  763. // 所有文件大小.
  764. FileInfo[] fis = d.GetFiles();
  765. foreach (FileInfo fi in fis)
  766. {
  767. Size += fi.Length;
  768. }
  769. // 遍历出当前目录的所有文件夹.
  770. DirectoryInfo[] dis = d.GetDirectories();
  771. foreach (DirectoryInfo di in dis)
  772. {
  773. Size += DirSize(di); //这就用到递归了,调用父方法,注意,这里并不是直接返回值,而是调用父返回来的
  774. }
  775. return (Size);
  776. }
  777. #endregion
  778. #region 移动目录
  779. /// <summary>
  780. /// 移动目录并重命名
  781. /// </summary>
  782. /// <param name="directoryPath">操作目录绝对路径</param>
  783. /// <param name="newDirectoryPath">目标目录绝对路径</param>
  784. /// <param name="Message">返回错误消息,成功则为空</param>
  785. /// <returns></returns>
  786. public static bool MoveDirectory(string directoryPath, string newDirectoryPath, out string Message)
  787. {
  788. Message = "";
  789. try
  790. {
  791. if (!IsExistDirectory(directoryPath))//判断目录是否存在
  792. {
  793. Message = "操作目录不存在!";
  794. return false;
  795. }
  796. if (IsExistDirectory(newDirectoryPath))
  797. {
  798. Message = "目标目录已存在!";
  799. return false;
  800. }
  801. Directory.Move(directoryPath, newDirectoryPath);
  802. return true;
  803. }
  804. catch (Exception ex)
  805. {
  806. Message = ex.Message;
  807. return false;
  808. }
  809. }
  810. #endregion
  811. #region 操作配置文件
  812. #region 从配置文件获取Value
  813. /// <summary>
  814. /// 从配置文件获取Value
  815. /// </summary>
  816. /// <param name="key">配置文件中key字符串</param>
  817. /// <returns></returns>
  818. public static string GetConfigValue(string key)
  819. {
  820. try
  821. {
  822. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  823. //获取AppSettings的节点
  824. AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
  825. return appsection.Settings[key].Value;
  826. }
  827. catch
  828. {
  829. return "";
  830. }
  831. }
  832. #endregion
  833. #region 设置配置文件
  834. /// <summary>
  835. /// 设置配置文件
  836. /// </summary>
  837. /// <param name="key">配置文件中key字符串</param>
  838. /// <param name="value">配置文件中value字符串</param>
  839. /// <returns></returns>
  840. public static bool SetConfigValue(string key, string value)
  841. {
  842. try
  843. {
  844. //打开配置文件
  845. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  846. //获取AppSettings的节点
  847. AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
  848. appsection.Settings[key].Value = value;
  849. config.Save();
  850. return true;
  851. }
  852. catch
  853. {
  854. return false;
  855. }
  856. }
  857. #endregion
  858. #endregion
  859. }
  860. }