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

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