星火微课系统客户端
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 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. stream.Read
  542. (
  543. buffer,
  544. 0,
  545. int.Parse(stream.Length.ToString())
  546. );
  547. //返回流
  548. return buffer;
  549. }
  550. catch
  551. {
  552. return null;
  553. }
  554. finally
  555. {
  556. //关闭流
  557. stream.Close();
  558. }
  559. }
  560. #endregion
  561. #region 将文件读取到缓冲区中
  562. /// <summary>
  563. /// 将文件读取到缓冲区中
  564. /// </summary>
  565. /// <param name="filePath">文件的绝对路径</param>
  566. public static byte[] FileToBytes(string filePath)
  567. {
  568. //获取文件的大小
  569. long fileSize = GetFileSize(filePath);
  570. //创建一个临时缓冲区
  571. byte[] buffer = new byte[fileSize];
  572. //创建一个文件流
  573. FileInfo fi = new FileInfo(filePath);
  574. FileStream fs = fi.Open(FileMode.Open);
  575. try
  576. {
  577. //将文件流读入缓冲区
  578. fs.Read
  579. (
  580. buffer,
  581. 0,
  582. (int)fileSize
  583. );
  584. return buffer;
  585. }
  586. catch
  587. {
  588. return null;
  589. }
  590. finally
  591. {
  592. //关闭文件流
  593. fs.Close();
  594. }
  595. }
  596. /// <summary>
  597. /// 读取大文件用,读取文件前面指定长度字节数
  598. /// </summary>
  599. /// <param name="filePath">文件路径</param>
  600. /// <param name="length">读取长度,单位字节</param>
  601. /// <returns></returns>
  602. public static byte[] ReadBigFile(string filePath, int length)
  603. {
  604. FileStream stream = new FileStream(filePath, FileMode.Open);
  605. byte[] buffer = new byte[length];
  606. //stream.Position = startIndex;
  607. stream.Read
  608. (
  609. buffer,
  610. 0,
  611. length
  612. );
  613. stream.Close();
  614. stream.Dispose();
  615. return buffer;
  616. }
  617. /// <summary>
  618. /// 读取指定长度的流
  619. /// </summary>
  620. /// <param name="filePath">文件位置</param>
  621. /// <param name="startIndex">开始位置</param>
  622. /// <param name="length">长度</param>
  623. /// <returns></returns>
  624. public static byte[] ReadBigFileSpecifyLength
  625. (
  626. string filePath,
  627. long startIndex,
  628. int length
  629. )
  630. {
  631. FileStream stream = new FileStream(filePath, FileMode.Open);
  632. if (startIndex + length + 1 > stream.Length)
  633. {
  634. length = (int)(stream.Length - startIndex);
  635. }
  636. byte[] buffer = new byte[length];
  637. stream.Position = startIndex;
  638. stream.Read
  639. (
  640. buffer,
  641. 0,
  642. length
  643. );
  644. stream.Close();
  645. stream.Dispose();
  646. return buffer;
  647. }
  648. #endregion
  649. #region 将文件读取到字符串中
  650. /// <summary>
  651. /// 将文件读取到字符串中
  652. /// </summary>
  653. /// <param name="filePath">文件的绝对路径</param>
  654. public static string FileToString(string filePath)
  655. {
  656. //return FileToString(filePath, Encoding.Default);
  657. return FileToString(filePath, Encoding.UTF8);
  658. }
  659. /// <summary>
  660. /// 将文件读取到字符串中
  661. /// </summary>
  662. /// <param name="filePath">文件的绝对路径</param>
  663. /// <param name="encoding">字符编码</param>
  664. public static string FileToString(string filePath, Encoding encoding)
  665. {
  666. //创建流读取器
  667. StreamReader reader = new StreamReader(filePath, encoding);
  668. try
  669. {
  670. //读取流
  671. return reader.ReadToEnd();
  672. }
  673. catch
  674. {
  675. return string.Empty;
  676. }
  677. finally
  678. {
  679. //关闭流读取器
  680. reader.Close();
  681. }
  682. }
  683. /// <summary>
  684. /// 读取大文件用,读取文件前面指定长度字节的字符串
  685. /// </summary>
  686. /// <param name="filePath">文件路径</param>
  687. /// <param name="length">读取长度,单位字节</param>
  688. /// <returns></returns>
  689. public static string ReadBigFileStr(string filePath, int length)
  690. {
  691. byte[] buffer = ReadBigFile(filePath, length);
  692. return Encoding.Default.GetString(buffer);
  693. }
  694. #endregion
  695. #region 从文件的绝对路径中获取文件名( 包含扩展名 )
  696. /// <summary>
  697. /// 从文件的绝对路径中获取文件名( 包含扩展名 )
  698. /// </summary>
  699. /// <param name="filePath">文件的绝对路径</param>
  700. public static string GetFileName(string filePath)
  701. {
  702. //获取文件的名称
  703. FileInfo fi = new FileInfo(filePath);
  704. return fi.Name;
  705. }
  706. /// <summary>
  707. /// 使用IO从文件的绝对路径中获取文件名( 包含扩展名 )
  708. /// </summary>
  709. /// <param name="filePath">文件的绝对路径</param>
  710. public static string GetIoFileName(string filePath)
  711. {
  712. return Path.GetFileName(filePath);
  713. }
  714. #endregion
  715. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  716. /// <summary>
  717. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  718. /// </summary>
  719. /// <param name="filePath">文件的绝对路径</param>
  720. public static string GetFileNameNoExtension(string filePath)
  721. {
  722. //获取文件的名称
  723. FileInfo fi = new FileInfo(filePath);
  724. return fi.Name.Split('.')[0];
  725. }
  726. /// <summary>
  727. /// 使用IO从文件的绝对路径中获取文件名( 不包含扩展名 )
  728. /// </summary>
  729. /// <param name="filePath">文件的绝对路径</param>
  730. public static string GetIoFileNameNoExtension(string filePath)
  731. {
  732. return System.IO.Path.GetFileNameWithoutExtension(filePath);
  733. }
  734. #endregion
  735. #region 从文件的绝对路径中获取扩展名
  736. /// <summary>
  737. /// 从文件的绝对路径中获取扩展名
  738. /// </summary>
  739. /// <param name="filePath">文件的绝对路径</param>
  740. public static string GetExtension(string filePath)
  741. {
  742. //获取文件的名称
  743. FileInfo fi = new FileInfo(filePath);
  744. return fi.Extension;
  745. }
  746. /// <summary>
  747. /// 使用IO从文件的绝对路径中获取扩展名
  748. /// </summary>
  749. /// <param name="filePath">文件的绝对路径</param>
  750. public static string GetIoExtension(string filePath)
  751. {
  752. string extension = System.IO.Path.GetExtension(filePath);
  753. return extension;
  754. }
  755. #endregion
  756. #region 从文件的绝对路径中获取路径名
  757. /// <summary>
  758. /// 使用IO从文件的绝对路径中获取路径名
  759. /// </summary>
  760. /// <param name="filePath">文件的绝对路径</param>
  761. public static string GetDirectoryName(string filePath)
  762. {
  763. string directoryName = (System.IO.Path.GetDirectoryName(filePath) + @"/").Replace("\\", "/");
  764. return directoryName;
  765. }
  766. #endregion
  767. #region 清空指定目录
  768. /// <summary>
  769. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  770. /// </summary>
  771. /// <param name="directoryPath">指定目录的绝对路径</param>
  772. public static void ClearDirectory(string directoryPath)
  773. {
  774. if (IsExistDirectory(directoryPath))
  775. {
  776. //删除目录中所有的文件
  777. string[] fileNames = GetFileNames(directoryPath);
  778. foreach (string t in fileNames)
  779. {
  780. DeleteFile(t);
  781. }
  782. //删除目录中所有的子目录
  783. string[] directoryNames = GetDirectories(directoryPath);
  784. foreach (string t in directoryNames)
  785. {
  786. DeleteDirectory(t);
  787. }
  788. }
  789. }
  790. #endregion
  791. #region 清空文件内容
  792. /// <summary>
  793. /// 清空文件内容
  794. /// </summary>
  795. /// <param name="filePath">文件的绝对路径</param>
  796. public static void ClearFile(string filePath)
  797. {
  798. //删除文件
  799. File.Delete(filePath);
  800. //重新创建该文件
  801. CreateFile(filePath);
  802. }
  803. #endregion
  804. #region 删除指定文件
  805. /// <summary>
  806. /// 删除指定文件
  807. /// </summary>
  808. /// <param name="filePath">文件的绝对路径</param>
  809. public static void DeleteFile(string filePath)
  810. {
  811. try
  812. {
  813. if (IsExistFile(filePath))
  814. {
  815. File.Delete(filePath);
  816. }
  817. }
  818. catch (Exception)
  819. {
  820. // ignored
  821. }
  822. }
  823. #endregion
  824. #region 删除指定目录
  825. /// <summary>
  826. /// 删除指定目录及其所有子目录
  827. /// </summary>
  828. /// <param name="directoryPath">指定目录的绝对路径</param>
  829. public static bool DeleteDirectory(string directoryPath)
  830. {
  831. try
  832. {
  833. if (IsExistDirectory(directoryPath))
  834. {
  835. Directory.Delete(directoryPath, true);
  836. }
  837. return true;
  838. }
  839. catch (Exception)
  840. {
  841. //目录中有文件占用
  842. return false;
  843. }
  844. }
  845. #endregion
  846. #region 获取指定目录大小
  847. /// <summary>
  848. /// 获取目录大小
  849. /// </summary>
  850. /// <param name="directoryPath">目录地址</param>
  851. /// <returns> 单位:MB</returns>
  852. public static double PathSize(string directoryPath)
  853. {
  854. DirectoryInfo folder = new DirectoryInfo(directoryPath);
  855. double directorySize = DirSize(folder);
  856. for (int i = 0; i < 2; i++)
  857. {
  858. directorySize /= 1024;
  859. }
  860. return directorySize;
  861. }
  862. /// <summary>
  863. /// 获取目录大小
  864. /// </summary>
  865. /// <param name="d">目录</param>
  866. /// <returns>字节</returns>
  867. public static long DirSize(DirectoryInfo d)
  868. {
  869. long size = 0;
  870. // 所有文件大小.
  871. FileInfo[] fis = d.GetFiles();
  872. foreach (FileInfo fi in fis)
  873. {
  874. size += fi.Length;
  875. }
  876. // 遍历出当前目录的所有文件夹.
  877. DirectoryInfo[] dis = d.GetDirectories();
  878. foreach (DirectoryInfo di in dis)
  879. {
  880. size += DirSize(di); //这就用到递归了,调用父方法,注意,这里并不是直接返回值,而是调用父返回来的
  881. }
  882. return (size);
  883. }
  884. #endregion
  885. #region 移动目录
  886. /// <summary>
  887. /// 移动目录并重命名
  888. /// </summary>
  889. /// <param name="directoryPath">操作目录绝对路径</param>
  890. /// <param name="newDirectoryPath">目标目录绝对路径</param>
  891. /// <param name="message">返回错误消息,成功则为空</param>
  892. /// <returns></returns>
  893. public static bool MoveDirectory
  894. (
  895. string directoryPath,
  896. string newDirectoryPath,
  897. out string message
  898. )
  899. {
  900. message = "";
  901. try
  902. {
  903. if (!IsExistDirectory(directoryPath)) //判断目录是否存在
  904. {
  905. message = "操作目录不存在!";
  906. return false;
  907. }
  908. if (IsExistDirectory(newDirectoryPath))
  909. {
  910. message = "目标目录已存在!";
  911. return false;
  912. }
  913. Directory.Move(directoryPath, newDirectoryPath);
  914. return true;
  915. }
  916. catch (Exception ex)
  917. {
  918. message = ex.Message;
  919. return false;
  920. }
  921. }
  922. #endregion
  923. #region 操作配置文件
  924. #region 从配置文件获取Value
  925. /// <summary>
  926. /// 从配置文件获取Value
  927. /// </summary>
  928. /// <param name="key">配置文件中key字符串</param>
  929. /// <returns></returns>
  930. public static string GetConfigValue(string key)
  931. {
  932. try
  933. {
  934. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  935. //获取AppSettings的节点
  936. AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
  937. return appsection.Settings[key].Value;
  938. }
  939. catch
  940. {
  941. return "";
  942. }
  943. }
  944. #endregion
  945. #region 设置配置文件
  946. /// <summary>
  947. /// 设置配置文件
  948. /// </summary>
  949. /// <param name="key">配置文件中key字符串</param>
  950. /// <param name="value">配置文件中value字符串</param>
  951. /// <returns></returns>
  952. public static bool SetConfigValue(string key, string value)
  953. {
  954. try
  955. {
  956. //打开配置文件
  957. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  958. //获取AppSettings的节点
  959. AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
  960. appsection.Settings[key].Value = value;
  961. config.Save();
  962. return true;
  963. }
  964. catch
  965. {
  966. return false;
  967. }
  968. }
  969. #endregion
  970. #endregion
  971. #region 获取媒体文件的播放时长
  972. #endregion
  973. #region 检测是否安装了某个程序
  974. /// <summary>
  975. /// 确认电脑上是否安装有某个程序
  976. /// </summary>
  977. /// <param name="softWareName">程序安装后的名称</param>
  978. /// <returns>true: 有安裝, false:沒有安裝</returns>
  979. public static bool CheckSoftWartInstallState(string softWareName)
  980. {
  981. Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey
  982. (
  983. @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
  984. Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree,
  985. System.Security.AccessControl.RegistryRights.ReadKey
  986. );
  987. if (uninstallNode != null)
  988. {
  989. foreach (string subKeyName in uninstallNode.GetSubKeyNames())
  990. {
  991. //Console.WriteLine(" subKeyName:" + subKeyName);
  992. Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
  993. if (subKey != null)
  994. {
  995. object displayName = subKey.GetValue("DisplayName");
  996. if (displayName != null)
  997. {
  998. if (displayName.ToString().Contains(softWareName))
  999. {
  1000. //Console.WriteLine(uninstallNode.Name+" subKeyName:" + subKeyName);
  1001. return true;
  1002. // MessageWindow.Show(displayName.ToString());
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. return false;
  1009. }
  1010. #endregion
  1011. }
  1012. }