星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FileToolsCommon.cs 32KB

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