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.

789 lines
29 KiB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Security.Permissions;
  7. namespace Apewer
  8. {
  9. /// <summary>存储实用工具。</summary>
  10. public static class StorageUtility
  11. {
  12. private static Exception Try(Action action)
  13. {
  14. try { action?.Invoke(); return null; }
  15. catch (Exception ex) { return ex; }
  16. }
  17. /// <summary>文件操作线程锁。</summary>
  18. public static object Locker = new object();
  19. #region NTFS 流
  20. /// <summary>获取标记的路径。</summary>
  21. public static string GetZoneIdentifier(string path) => NtfsUnlocker.GetZoneIdentifier(path);
  22. /// <summary>检查标记是否存在。</summary>
  23. public static bool ZoneIdentifierExists(string zoneIdentifier) => NtfsUnlocker.ZoneIdentifierExists(zoneIdentifier);
  24. /// <summary>删除标记。</summary>
  25. public static bool DeleteZoneIdentifier(string zoneIdentifier) => NtfsUnlocker.DeleteZoneIdentifier(zoneIdentifier);
  26. #endregion
  27. #region delete
  28. /// <summary>删除文件。</summary>
  29. public static Exception DeleteFile(string path, bool useTemp = false)
  30. {
  31. if (useTemp)
  32. {
  33. try
  34. {
  35. if (string.IsNullOrEmpty(path)) return new ArgumentException();
  36. if (!File.Exists(path)) return new FileNotFoundException();
  37. Try(() => new FileInfo(path).Attributes = FileAttributes.Normal);
  38. var temp = Environment.GetEnvironmentVariable("TEMP");
  39. var name = Path.GetFileName(path);
  40. var dest = null as string;
  41. while (dest == null || File.Exists(dest))
  42. {
  43. var guid = Guid.NewGuid().ToString("n").Substring(0, 8);
  44. dest = $"trash_{guid}_{name}";
  45. }
  46. File.Move(path, dest);
  47. File.Delete(dest);
  48. return null;
  49. }
  50. catch (Exception ex) { return ex; }
  51. }
  52. try
  53. {
  54. File.Delete(path);
  55. return null;
  56. }
  57. catch (Exception ex) { return ex; }
  58. }
  59. /// <summary>删除目录、子目录和子文件。</summary>
  60. public static Exception DeleteDirectory(string path, bool useTemp = false)
  61. {
  62. if (useTemp)
  63. {
  64. try
  65. {
  66. if (string.IsNullOrEmpty(path)) return new ArgumentException();
  67. if (!Directory.Exists(path)) return new DirectoryNotFoundException();
  68. var temp = Environment.GetEnvironmentVariable("TEMP");
  69. var name = Path.GetDirectoryName(path);
  70. var dest = null as string;
  71. while (dest == null || Directory.Exists(dest))
  72. {
  73. var guid = Guid.NewGuid().ToString("n").Substring(0, 8);
  74. dest = $"trash_{guid}_{name}";
  75. }
  76. Directory.Move(path, dest);
  77. Directory.Delete(dest, true);
  78. return null;
  79. }
  80. catch (Exception ex) { return ex; }
  81. }
  82. try
  83. {
  84. Directory.Delete(path, true);
  85. return null;
  86. }
  87. catch (Exception ex) { return ex; }
  88. }
  89. #endregion
  90. #region path
  91. /// <summary>无效的路径字符。</summary>
  92. public static char[] InvalidPathChars
  93. {
  94. // SMB 共享文件夹无效字符
  95. // ! " # % & ' ( ) * + , / : ; < = > ? @ [ ] \ ^ ` { } | ~
  96. get => new char[] {
  97. '\\', '/', '\'', '"', ':', '*', '?', '<', '>', '|',
  98. '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r',
  99. '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u000e', '\u000f',
  100. '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017',
  101. '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001e', '\u001f'
  102. };
  103. }
  104. /// <summary>合并路径。</summary>
  105. public static string CombinePath(params string[] paths)
  106. {
  107. if (paths == null || paths.Length < 1) return "";
  108. try
  109. {
  110. #if NET20
  111. var result = paths[0];
  112. for (var i = 0; i < paths.Length; i++)
  113. {
  114. result = Path.Combine(result, paths[i]);
  115. }
  116. return result;
  117. #else
  118. return Path.Combine(paths);
  119. #endif
  120. }
  121. catch { return ""; }
  122. }
  123. /// <summary>获取文件或目录的存在状态。</summary>
  124. public static bool PathExists(string path)
  125. {
  126. if (string.IsNullOrEmpty(path)) return false;
  127. if (File.Exists(path)) return true;
  128. if (Directory.Exists(path)) return true;
  129. return false;
  130. }
  131. /// <summary>获取目录的存在状态。</summary>
  132. public static bool DirectoryExists(string path) => string.IsNullOrEmpty(path) ? false : Directory.Exists(path);
  133. /// <summary>获取文件的存在状态。</summary>
  134. public static bool FileExists(string path) => string.IsNullOrEmpty(path) ? false : File.Exists(path);
  135. /// <summary>获取文件或目录的上级目录完整路径,失败时返回 NULL 值。。</summary>
  136. public static string GetParentPath(string path)
  137. {
  138. try
  139. {
  140. if (File.Exists(path)) return Path.GetDirectoryName(path);
  141. if (Directory.Exists(path)) return Directory.GetParent(path).FullName;
  142. }
  143. catch { }
  144. return null;
  145. }
  146. /// <summary>修正文件名,去除不允许的字符。</summary>
  147. public static string FixFileName(string fileName)
  148. {
  149. var result = fileName;
  150. if (string.IsNullOrEmpty(result))
  151. {
  152. result = Constant.EmptyString;
  153. }
  154. else
  155. {
  156. var invalid = InvalidPathChars;
  157. foreach (var c in invalid) result = result.Replace(c.ToString(), "");
  158. }
  159. if (result.Length > 0) result = result.Trim();
  160. return result;
  161. }
  162. /// <summary>获取指定目录的子目录,可指定递归子目录。</summary>
  163. public static List<string> GetSubDirectories(string dirPath, bool recursive = false, bool recurPrecedence = true) => GetSubDirectories(dirPath, recursive ? -1 : 0, recurPrecedence);
  164. /// <summary>获取指定目录下子目录的路径。</summary>
  165. /// <param name="dirPath">顶级目录。</param>
  166. /// <param name="recurDepth">子目录递归深度,指定为 0 时不递归,指定为 -1 时无限递归。</param>
  167. /// <param name="recurPrecedence">优先排列递归项。</param>
  168. public static List<string> GetSubDirectories(string dirPath, int recurDepth, bool recurPrecedence = true)
  169. {
  170. var list = new List<string>();
  171. if (string.IsNullOrEmpty(dirPath)) return list;
  172. var directories = new string[0];
  173. try
  174. {
  175. directories = Directory.GetDirectories(dirPath);
  176. }
  177. catch { }
  178. foreach (var dir in directories)
  179. {
  180. var recurlist = new List<string>();
  181. if (recurDepth != 0)
  182. {
  183. var depth = (recurDepth > 0) ? recurDepth - 1 : recurDepth;
  184. var subrecur = GetSubDirectories(dir, depth, recurPrecedence);
  185. recurlist.AddRange(subrecur);
  186. }
  187. if (recurPrecedence)
  188. {
  189. list.AddRange(recurlist);
  190. list.Add(dir);
  191. }
  192. else
  193. {
  194. list.Add(dir);
  195. list.AddRange(recurlist);
  196. }
  197. }
  198. return list;
  199. }
  200. /// <summary>获取指定目录的子文件。</summary>
  201. /// <param name="dirPath">要查询的目录。</param>
  202. /// <param name="recursive">递归子目录。</param>
  203. /// <param name="recurPrecedence">优先排列递归项。</param>
  204. public static List<string> GetSubFiles(string dirPath, bool recursive = false, bool recurPrecedence = true) => GetSubFiles(dirPath, recursive ? -1 : 0, recurPrecedence);
  205. /// <summary>获取指定目录下子文件的路径。</summary>
  206. /// <param name="dirPath">要查询的目录。</param>
  207. /// <param name="recurDepth">子目录递归深度,指定为 0 时不递归,指定为 -1 时无限递归。</param>
  208. /// <param name="recurPrecedence">优先排列递归项。</param>
  209. public static List<string> GetSubFiles(string dirPath, int recurDepth, bool recurPrecedence = true)
  210. {
  211. var list = new List<string>();
  212. if (string.IsNullOrEmpty(dirPath)) return list;
  213. var dirs = new List<string>();
  214. if (recurDepth == 0)
  215. {
  216. dirs.Add(dirPath);
  217. }
  218. else
  219. {
  220. var recurdicrotylist = GetSubDirectories(dirPath, recurDepth, recurPrecedence);
  221. if (recurPrecedence)
  222. {
  223. dirs.AddRange(recurdicrotylist);
  224. dirs.Add(dirPath);
  225. }
  226. else
  227. {
  228. dirs.Add(dirPath);
  229. dirs.AddRange(recurdicrotylist);
  230. }
  231. }
  232. foreach (var d in dirs)
  233. {
  234. try
  235. {
  236. var files = System.IO.Directory.GetFiles(d);
  237. list.AddRange(files);
  238. }
  239. catch { }
  240. }
  241. return list;
  242. }
  243. #endregion
  244. #region directory
  245. /// <summary>确信指定存在指定目录。</summary>
  246. public static bool AssureDirectory(string path)
  247. {
  248. if (string.IsNullOrEmpty(path)) return false;
  249. try
  250. {
  251. if (File.Exists(path)) return false;
  252. if (Directory.Exists(path)) return true;
  253. var created = Directory.CreateDirectory(path);
  254. return created.Exists;
  255. }
  256. catch { }
  257. return false;
  258. }
  259. /// <summary>确信指定存在指定路径所在的上级目录。</summary>
  260. public static bool AssureParent(string path)
  261. {
  262. if (string.IsNullOrEmpty(path)) return false;
  263. var parent = Constant.EmptyString;
  264. try { parent = Directory.GetParent(path).FullName; } catch { }
  265. var result = AssureDirectory(parent);
  266. return result;
  267. }
  268. #endregion
  269. #region file
  270. /// <summary>打开文件,并获取文件流。若文件不存在,则先创建文件;若获取失败,则返回 NULL 值。可选文件的锁定状态。</summary>
  271. public static FileStream OpenFile(string path, bool share = true)
  272. {
  273. try
  274. {
  275. if (string.IsNullOrEmpty(path)) return null;
  276. if (DirectoryExists(path)) return null;
  277. if (AssureParent(path))
  278. {
  279. var m = FileMode.OpenOrCreate;
  280. var a = FileAccess.ReadWrite;
  281. var s = share ? FileShare.ReadWrite : FileShare.None;
  282. var stream = new FileStream(path, m, a, s);
  283. return stream;
  284. }
  285. }
  286. catch { }
  287. return null;
  288. }
  289. /// <summary>确保指定路径存在文件,若不存在则新建。</summary>
  290. /// <param name="path">文件路径。</param>
  291. public static bool AssureFile(string path)
  292. {
  293. if (string.IsNullOrEmpty(path)) return false;
  294. if (File.Exists(path)) return true;
  295. if (Directory.Exists(path)) return false;
  296. if (!AssureParent(path)) return false;
  297. try
  298. {
  299. File.Create(path).Dispose();
  300. return true;
  301. }
  302. catch
  303. {
  304. return false;
  305. }
  306. }
  307. /// <summary>创建一个空文件且不保留句柄。</summary>
  308. /// <param name="path">文件路径,若已存在则返回失败。</param>
  309. /// <param name="length">文件长度(字节数)。</param>
  310. /// <param name="replace">替换现有文件。</param>
  311. /// <returns>创建成功。</returns>
  312. public static bool CreateFile(string path, long length = 0, bool replace = false)
  313. {
  314. if (string.IsNullOrEmpty(path)) return false;
  315. if (!replace && File.Exists(path)) return false;
  316. if (!AssureParent(path)) return false;
  317. lock (Locker)
  318. {
  319. var file = null as FileStream;
  320. var success = false;
  321. try
  322. {
  323. var m = replace ? FileMode.Create : FileMode.OpenOrCreate;
  324. file = new FileStream(path, m, FileAccess.ReadWrite, FileShare.ReadWrite);
  325. if (length > 0) file.SetLength(length);
  326. success = true;
  327. }
  328. catch { }
  329. RuntimeUtility.Dispose(file);
  330. return success;
  331. }
  332. }
  333. /// <summary>复制文件。</summary>
  334. /// <param name="source">旧路径。</param>
  335. /// <param name="destination">新路径。</param>
  336. /// <param name="replace">新路径存在时,替换新文件。</param>
  337. public static bool CopyFile(string source, string destination, bool replace = true)
  338. {
  339. if (string.IsNullOrEmpty(source)) return false;
  340. if (string.IsNullOrEmpty(destination)) return false;
  341. lock (Locker)
  342. {
  343. if (!FileExists(source)) return false;
  344. try
  345. {
  346. File.Copy(source, destination, replace);
  347. return true;
  348. }
  349. catch { return false; }
  350. }
  351. }
  352. /// <summary>向文件追加数据。文件不存在时将创建。</summary>
  353. public static bool AppendFile(string path, params byte[] bytes)
  354. {
  355. if (bytes == null || bytes.Length < 1) return false;
  356. lock (Locker)
  357. {
  358. var assured = AssureParent(path);
  359. if (!assured) return false;
  360. using (var file = OpenFile(path, true))
  361. {
  362. if (file == null) return false;
  363. try
  364. {
  365. file.Position = file.Length;
  366. file.Write(bytes, 0, bytes.Length);
  367. file.Flush();
  368. return true;
  369. }
  370. catch { return false; }
  371. }
  372. }
  373. }
  374. /// <summary>将数据写入新文件,若文件已存在则覆盖。</summary>
  375. public static bool WriteFile(string path, params byte[] bytes) => WriteFile(path, false, bytes);
  376. /// <summary>将数据写入新文件,若文件已存在则覆盖。</summary>
  377. public static bool WriteFile(string path, bool bom, params byte[] bytes)
  378. {
  379. if (string.IsNullOrEmpty(path)) return false;
  380. if (bom)
  381. {
  382. lock (Locker)
  383. {
  384. if (bytes == null || bytes.Length < 1)
  385. {
  386. try
  387. {
  388. var dir = Path.GetDirectoryName(path);
  389. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  390. File.WriteAllBytes(path, TextUtility.Bom);
  391. return true;
  392. }
  393. catch
  394. {
  395. return false;
  396. }
  397. }
  398. else
  399. {
  400. var file = OpenFile(path, true);
  401. var success = false;
  402. try
  403. {
  404. var dir = Path.GetDirectoryName(path);
  405. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  406. var write1 = BytesUtility.Write(file, TextUtility.Bom);
  407. if (write1 == TextUtility.Bom.Length)
  408. {
  409. var write2 = BytesUtility.Write(file, bytes);
  410. if (bytes != null && bytes.LongLength > 0L)
  411. {
  412. }
  413. success = true;
  414. }
  415. }
  416. catch { }
  417. RuntimeUtility.Dispose(file);
  418. return success;
  419. }
  420. }
  421. }
  422. else
  423. {
  424. lock (Locker)
  425. {
  426. try
  427. {
  428. var dir = Path.GetDirectoryName(path);
  429. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  430. File.WriteAllBytes(path, bytes);
  431. return true;
  432. }
  433. catch
  434. {
  435. return false;
  436. }
  437. }
  438. }
  439. }
  440. /// <summary>读取文件,获取文件内容。当文件为 UTF-8 文本文件时,可去除 BOM 头。</summary>
  441. /// <remarks>注:<br />字节数组最大为 2GB;<br />此方法不抛出异常,读取失时返回空字节数组。</remarks>
  442. public static byte[] ReadFile(string path, bool wipeBom = false)
  443. {
  444. const int Buffer = 1048576;
  445. if (!FileExists(path)) return Constant.EmptyBytes;
  446. lock (Locker)
  447. {
  448. try
  449. {
  450. var result = new byte[0];
  451. using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
  452. {
  453. if (!stream.CanRead) return result;
  454. stream.Position = 0;
  455. var length = stream.Length;
  456. if (length < 1) return result;
  457. if (length > int.MaxValue) return result;
  458. var offset = 0L;
  459. if (wipeBom && length >= 3)
  460. {
  461. var head = new byte[3];
  462. stream.Read(head, 0, 3);
  463. if (TextUtility.ContainsBOM(head))
  464. {
  465. var capacity = length - 3;
  466. result = new byte[capacity];
  467. }
  468. else
  469. {
  470. result = new byte[length];
  471. result[0] = head[0];
  472. result[1] = head[1];
  473. result[2] = head[2];
  474. offset = 3;
  475. }
  476. }
  477. else
  478. {
  479. result = new byte[length];
  480. }
  481. var block = new byte[Buffer];
  482. while (true)
  483. {
  484. var read = stream.Read(block, 0, Buffer);
  485. if (read < 1) break;
  486. Array.Copy(block, 0, result, offset, read);
  487. offset += read;
  488. }
  489. }
  490. return result;
  491. }
  492. catch { }
  493. }
  494. return Constant.EmptyBytes;
  495. }
  496. /// <summary>获取指定文件的字节数,获取失败时返回 -1 值。</summary>
  497. public static long GetFileLength(string path)
  498. {
  499. try
  500. {
  501. var info = new FileInfo(path);
  502. return info.Length;
  503. }
  504. catch { }
  505. return -1;
  506. }
  507. #endregion
  508. /// <summary>获取文件的最后写入时间。</summary>
  509. public static DateTime GetFileLastWriteTime(string path, bool utc = false)
  510. {
  511. try
  512. {
  513. var info = new FileInfo(path);
  514. return utc ? info.LastWriteTimeUtc : info.LastWriteTime;
  515. }
  516. catch { }
  517. return new DateTime();
  518. }
  519. /// <summary>设置文件的最后写入时间。</summary>
  520. public static string SetFileLastWriteTime(string path, DateTime value)
  521. {
  522. try
  523. {
  524. var info = new FileInfo(path);
  525. info.LastWriteTime = value;
  526. return null;
  527. }
  528. catch (Exception ex)
  529. {
  530. return ex.Message;
  531. }
  532. }
  533. /// <summary>压缩文件到流。</summary>
  534. /// <param name="files">Key 为 ZIP 内的文件名,Value 为原始文件路径。</param>
  535. /// <param name="output">要输出的 ZIP 流。</param>
  536. public static Exception ToZip(Dictionary<string, string> files, Stream output)
  537. {
  538. if (files == null) return new ArgumentNullException(nameof(files));
  539. var streams = new List<Stream>();
  540. var ex = BytesUtility.ToZip(files.Keys, output, (name) =>
  541. {
  542. if (name.IsEmpty()) return null;
  543. var path = files[name];
  544. if (path.IsEmpty()) return null;
  545. if (!FileExists(path)) return null;
  546. var input = OpenFile(path);
  547. streams.Add(input);
  548. return input;
  549. }, (name) => GetFileLastWriteTime(files[name]), true);
  550. RuntimeUtility.Dispose(streams);
  551. return ex;
  552. }
  553. /// <summary>压缩文件到流。</summary>
  554. /// <param name="files">Key 为 ZIP 内的文件名,Value 为原始文件路径。</param>
  555. /// <param name="output">要输出的 ZIP 文件路径,已存在的文件将被删除。</param>
  556. public static Exception ToZip(Dictionary<string, string> files, string output)
  557. {
  558. if (output.IsEmpty()) return new ArgumentException(nameof(output));
  559. if (FileExists(output)) DeleteFile(output);
  560. if (FileExists(output)) return new IOException();
  561. var stream = OpenFile(output);
  562. if (stream == null) return new IOException();
  563. var ex = ToZip(files, stream);
  564. RuntimeUtility.Dispose(stream, true);
  565. return ex;
  566. }
  567. /// <summary>压缩文件到流。</summary>
  568. /// <param name="directory">原始文件目录。</param>
  569. /// <param name="output">要输出的 ZIP 流。</param>
  570. public static Exception ToZip(string directory, Stream output)
  571. {
  572. if (!DirectoryExists(directory)) return new DirectoryNotFoundException();
  573. var dict = new Dictionary<string, string>();
  574. foreach (var path in GetSubFiles(directory, true))
  575. {
  576. var name = path.Substring(directory.Length);
  577. if (name.StartsWith("\\")) name = name.Substring(1);
  578. if (name.StartsWith("/")) name = name.Substring(1);
  579. dict.Add(name, path);
  580. }
  581. return ToZip(dict, output);
  582. }
  583. /// <summary>压缩文件到流。</summary>
  584. /// <param name="directory">原始文件目录。</param>
  585. /// <param name="output">要输出的 ZIP 文件路径,已存在的旧文件将被删除。</param>
  586. public static Exception ToZip(string directory, string output)
  587. {
  588. if (output.IsEmpty()) return new ArgumentException(nameof(output));
  589. if (FileExists(output)) DeleteFile(output);
  590. if (FileExists(output)) return new IOException("无法删除现有文件。");
  591. var stream = OpenFile(output);
  592. if (stream == null) return new IOException("无法创建文件。");
  593. var ex = ToZip(directory, stream);
  594. RuntimeUtility.Dispose(stream, true);
  595. return ex;
  596. }
  597. /// <summary>解压 ZIP 文件到目标目录。已存在的旧文件将被删除。</summary>
  598. public static Exception FromZip(string zipPath, string outputDirectory = null)
  599. {
  600. if (!FileExists(zipPath)) return new FileNotFoundException("指定的 ZIP 文件路径无效。");
  601. var directory = outputDirectory;
  602. if (directory.IsEmpty())
  603. {
  604. if (zipPath.Lower().EndsWith(".zip"))
  605. {
  606. directory = zipPath.Substring(0, zipPath.Length - 4);
  607. //if (directory.EndsWith("/") || directory.EndsWith("\\"))
  608. //if (PathExists(directory)) directory = null;
  609. }
  610. else
  611. {
  612. directory = GetParentPath(zipPath);
  613. }
  614. }
  615. if (directory.IsEmpty()) return new ArgumentException("无法判断目录路径。");
  616. if (!AssureDirectory(directory)) return new DirectoryNotFoundException("无法创建目录。");
  617. var input = OpenFile(zipPath);
  618. if (input == null) return new IOException("无法打开 ZIP 文件。");
  619. var info = new Dictionary<string, DateTime>();
  620. var ex = BytesUtility.FromZip(input, (name, size, modifield) =>
  621. {
  622. var path = Path.Combine(directory, name);
  623. if (FileExists(path)) DeleteFile(path);
  624. if (DirectoryExists(path)) DeleteDirectory(path, true);
  625. if (PathExists(path)) throw new IOException("无法删除旧文件或旧目录。");
  626. var output = OpenFile(path);
  627. if (output == null) throw new IOException("无法创建文件 " + path + "。");
  628. if (info.ContainsKey(path))
  629. {
  630. RuntimeUtility.Dispose(output);
  631. return null;
  632. }
  633. info.Add(path, modifield);
  634. return output;
  635. }, (name, modified) =>
  636. {
  637. var path = Path.Combine(directory, name);
  638. var exists = AssureDirectory(path);
  639. if (!exists) throw new IOException("无法创建 ZIP 中对应的目录。");
  640. }, true);
  641. // 恢复文件的修改时间。
  642. foreach (var i in info) SetFileLastWriteTime(i.Key, i.Value);
  643. return ex;
  644. }
  645. ///// <summary>解压 ZIP 文件到字典。失败且不允许异常时返回 NULL 值。</summary>
  646. //public static Dictionary<string, byte[]> FromZip(string zipPath, bool allowException = false)
  647. //{
  648. // if (FileExists(zipPath))
  649. // {
  650. // var ex = new FileNotFoundException("文件不存在。");
  651. // if (allowException) throw ex;
  652. // return null;
  653. // }
  654. // var input = OpenFile(zipPath);
  655. // if (input == null)
  656. // {
  657. // var ex = new IOException("无法打开 ZIP 文件。");
  658. // if (allowException) throw ex;
  659. // return null;
  660. // }
  661. // return BinaryUtility.FromZip(input, ;
  662. //}
  663. /// <summary>获取指定程序集的资源。</summary>
  664. public static Stream GetResource(string name, Assembly assembly = null)
  665. {
  666. try
  667. {
  668. if (assembly == null) assembly = Assembly.GetExecutingAssembly();
  669. var stream = assembly.GetManifestResourceStream(name);
  670. return stream;
  671. }
  672. catch { }
  673. return null;
  674. }
  675. /// <summary>获取当前进程程序集的资源。读取失败时返回 NULL 值。</summary>
  676. public static byte[] ReadResource(string name, Assembly assembly = null)
  677. {
  678. var stream = GetResource(name, assembly);
  679. if (stream == null) return null;
  680. var bytes = BytesUtility.Read(stream, true);
  681. return bytes;
  682. }
  683. /// <summary>监视文件夹的变化。</summary>
  684. public static FileSystemWatcher NewFileSystemWatcher(string directory, FileSystemEventHandler action)
  685. {
  686. var watcher = new FileSystemWatcher();
  687. watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.Size;
  688. watcher.IncludeSubdirectories = true;
  689. watcher.Path = directory;
  690. watcher.Created += (s, e) => action?.Invoke(watcher, e);
  691. watcher.Deleted += (s, e) => action?.Invoke(watcher, e);
  692. watcher.Changed += (s, e) => action?.Invoke(watcher, e);
  693. watcher.Renamed += (s, e) => action?.Invoke(watcher, e);
  694. watcher.EnableRaisingEvents = true;
  695. return watcher;
  696. }
  697. }
  698. }