星火微课系统客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileToolsCommon.cs 34KB

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