星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FileToolsCommon.cs 36KB

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