VisionProFileHelper.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. using Cognex.VisionPro;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace TeamAAS_VP.Core
  9. {
  10. /// <summary>
  11. /// VisionPro 文件操作辅助类:提供安全保存、加载、备份、恢复与验证等功能。
  12. /// 该类旨在通过临时文件、备份与验证机制减少文件损坏风险并支持自动恢复。
  13. /// </summary>
  14. public static class VisionProFileHelper
  15. {
  16. #region 配置选项
  17. /// <summary>
  18. /// VisionPro 文件处理的配置项。
  19. /// - EnableBackup: 是否启用备份机制
  20. /// - KeepBackupFiles: 是否保留版本化备份文件(否则仅保留 .bak 临时备份)
  21. /// - MaxBackupVersions: 保留的最大版本备份数量(超过则删除最旧)
  22. /// - BackupDirectory: 存放版本备份的目录名(相对于原文件所在目录)
  23. /// - ValidateAfterSave: 保存后是否验证文件完整性
  24. /// </summary>
  25. public class VisionProOptions
  26. {
  27. public bool EnableBackup { get; set; } = true;
  28. public bool KeepBackupFiles { get; set; } = false;
  29. public int MaxBackupVersions { get; set; } = 3;
  30. public string BackupDirectory { get; set; } = "VisionProBackups";
  31. public bool ValidateAfterSave { get; set; } = true;
  32. }
  33. private static VisionProOptions _options = new VisionProOptions();
  34. /// <summary>
  35. /// 配置 VisionPro 文件处理选项。
  36. /// </summary>
  37. /// <param name="configure">用于配置 VisionProOptions 的委托(可为 null)</param>
  38. public static void Configure(Action<VisionProOptions> configure)
  39. {
  40. configure?.Invoke(_options);
  41. }
  42. #endregion
  43. #region 安全保存方法
  44. /// <summary>
  45. /// 安全保存 VisionPro 对象到指定文件路径。
  46. /// 实现要点:
  47. /// - 支持在保存前根据配置创建版本化备份或 .bak 备份
  48. /// - 先保存到临时文件,再进行原子替换(File.Replace 或 File.Move)
  49. /// - 可选的保存后验证,验证失败会抛出异常并尝试从备份恢复
  50. /// </summary>
  51. /// <param name="obj">要保存的 VisionPro 对象(不能为空)</param>
  52. /// <param name="filePath">目标文件路径(不能为空或空白)</param>
  53. /// <param name="backupEnabled">可选:覆盖全局备份配置</param>
  54. /// <returns>成功返回 true,如发生异常会抛出 IOException</returns>
  55. public static bool SaveObjectToFileSafe(object obj, string filePath, bool? backupEnabled = null)
  56. {
  57. if (obj == null)
  58. throw new ArgumentNullException(nameof(obj));
  59. if (string.IsNullOrWhiteSpace(filePath))
  60. throw new ArgumentException("文件路径不能为空", nameof(filePath));
  61. bool useBackup = backupEnabled ?? _options.EnableBackup;
  62. // 创建目录(若不存在)
  63. string directory = Path.GetDirectoryName(filePath);
  64. if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
  65. {
  66. Directory.CreateDirectory(directory);
  67. }
  68. // 生成临时文件路径用于先写入
  69. string tempFilePath = GetVisionProTempFilePath(filePath);
  70. try
  71. {
  72. // 如果启用备份且目标文件已存在,则创建备份(版本化或延续 .bak 机制)
  73. if (useBackup && File.Exists(filePath))
  74. {
  75. CreateVisionProBackup(filePath);
  76. }
  77. // 将对象保存到临时文件
  78. CogSerializer.SaveObjectToFile(obj, tempFilePath);
  79. // 可选:验证临时文件的完整性
  80. if (_options.ValidateAfterSave && !ValidateVisionProFile(tempFilePath))
  81. {
  82. throw new InvalidOperationException("保存的VisionPro文件验证失败");
  83. }
  84. // 原子性替换:如果目标存在使用 File.Replace(可生成 .bak),否则直接移动临时文件
  85. if (File.Exists(filePath))
  86. {
  87. string backupPath = filePath + ".bak";
  88. File.Replace(tempFilePath, filePath, backupPath, true);
  89. // 若配置不保留备份,删除 .bak 文件
  90. if (!_options.KeepBackupFiles && File.Exists(backupPath))
  91. {
  92. SafeDelete(backupPath);
  93. }
  94. }
  95. else
  96. {
  97. File.Move(tempFilePath, filePath);
  98. }
  99. Log($"成功保存VisionPro对象到: {filePath}");
  100. return true;
  101. }
  102. catch (Exception ex)
  103. {
  104. Log($"保存VisionPro对象失败: {ex.Message}");
  105. // 尝试从备份恢复(若启用了备份)
  106. if (useBackup)
  107. {
  108. Log("尝试从备份恢复...");
  109. TryRestoreVisionProFile(filePath);
  110. }
  111. throw new IOException($"保存VisionPro文件失败: {ex.Message}", ex);
  112. }
  113. finally
  114. {
  115. // 无论成功与否,尝试清理临时文件
  116. SafeDelete(tempFilePath);
  117. }
  118. }
  119. /// <summary>
  120. /// 安全加载 VisionPro 对象(带自动恢复机制)。
  121. /// 尝试加载主文件,若失败并且允许自动恢复则尝试从 .bak 或版本备份恢复。
  122. /// </summary>
  123. /// <typeparam name="T">目标对象类型</typeparam>
  124. /// <param name="filePath">文件路径</param>
  125. /// <param name="autoRecover">是否启用自动恢复</param>
  126. /// <returns>加载得到的对象(成功),否则抛出 FileNotFoundException</returns>
  127. public static T LoadObjectFromFileSafe<T>(string filePath, bool autoRecover = true) where T : class
  128. {
  129. if (string.IsNullOrWhiteSpace(filePath))
  130. throw new ArgumentException("文件路径不能为空", nameof(filePath));
  131. // 清理可能残留的临时文件
  132. CleanVisionProTempFile(filePath);
  133. // 记录最后一次异常用于抛出上下文
  134. Exception lastException = null;
  135. try
  136. {
  137. if (File.Exists(filePath))
  138. {
  139. var obj = CogSerializer.LoadObjectFromFile(filePath) as T;
  140. if (obj == null)
  141. throw new InvalidCastException($"文件 {filePath} 不是有效的 {typeof(T).Name} 类型");
  142. return obj;
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. lastException = ex;
  148. Log($"加载主文件失败: {ex.Message}");
  149. if (autoRecover)
  150. {
  151. try
  152. {
  153. // 尝试从 .bak 或其他备份恢复并返回对象
  154. T recovered = TryRecoverVisionProFile<T>(filePath);
  155. if (recovered != null)
  156. return recovered;
  157. }
  158. catch (Exception recoveryEx)
  159. {
  160. Log($"恢复尝试失败: {recoveryEx.Message}");
  161. }
  162. }
  163. }
  164. // 如果主文件不存在且允许恢复,尝试查找版本化备份进行恢复
  165. if (autoRecover && !File.Exists(filePath))
  166. {
  167. T recovered = TryFindAndRecoverVisionProFile<T>(filePath);
  168. if (recovered != null)
  169. return recovered;
  170. }
  171. throw new FileNotFoundException($"VisionPro文件 {filePath} 不存在且无法恢复", lastException);
  172. }
  173. /// <summary>
  174. /// 简化版安全加载:遇到任何异常时返回提供的默认值(不抛出)。
  175. /// </summary>
  176. public static T SafeLoadObjectFromFile<T>(string filePath, T defaultValue = null) where T : class
  177. {
  178. try
  179. {
  180. return LoadObjectFromFileSafe<T>(filePath, true);
  181. }
  182. catch
  183. {
  184. return defaultValue;
  185. }
  186. }
  187. #endregion
  188. #region 恢复和验证方法
  189. /// <summary>
  190. /// 检查并修复单个 VisionPro 文件:
  191. /// - 检查并尝试从临时文件恢复
  192. /// - 检查 .bak 备份并在合理时恢复
  193. /// - 检查版本化备份并恢复最新的可用版本
  194. /// </summary>
  195. /// <param name="filePath">要检查的文件路径</param>
  196. /// <returns>如果执行了恢复操作返回 true,否则返回 false</returns>
  197. public static bool CheckAndRepairVisionProFile(string filePath)
  198. {
  199. try
  200. {
  201. // 检查临时文件并尝试恢复
  202. string tempPath = filePath + ".vp_tmp";
  203. if (File.Exists(tempPath))
  204. {
  205. TryRecoverFromVisionProTemp(tempPath);
  206. }
  207. // 检查 .bak 备份(File.Replace 可能产生)
  208. string backupPath = filePath + ".bak";
  209. if (File.Exists(backupPath))
  210. {
  211. // 验证备份文件并在主文件不存在或备份更新时恢复
  212. if (ValidateVisionProFile(backupPath))
  213. {
  214. if (!File.Exists(filePath) ||
  215. File.GetLastWriteTime(backupPath) > File.GetLastWriteTime(filePath))
  216. {
  217. File.Copy(backupPath, filePath, true);
  218. Log($"从备份恢复VisionPro文件: {filePath}");
  219. return true;
  220. }
  221. }
  222. }
  223. // 查找并恢复版本化备份(BackupDirectory 或其他模式)
  224. var latestBackup = FindLatestVisionProBackup(filePath);
  225. if (latestBackup != null)
  226. {
  227. File.Copy(latestBackup, filePath, true);
  228. Log($"从版本备份恢复VisionPro文件: {filePath}");
  229. return true;
  230. }
  231. return false;
  232. }
  233. catch (Exception ex)
  234. {
  235. Log($"修复VisionPro文件失败: {ex.Message}");
  236. return false;
  237. }
  238. }
  239. /// <summary>
  240. /// 强制从备份恢复(优先 .bak,然后版本化备份)。
  241. /// </summary>
  242. /// <param name="filePath">目标文件路径</param>
  243. /// <returns>恢复成功返回 true,否则 false</returns>
  244. public static bool ForceRecoverVisionProFile(string filePath)
  245. {
  246. try
  247. {
  248. // 优先使用 .bak 恢复(如存在且验证通过)
  249. string backupPath = filePath + ".bak";
  250. if (File.Exists(backupPath) && ValidateVisionProFile(backupPath))
  251. {
  252. File.Copy(backupPath, filePath, true);
  253. Log($"强制从备份恢复: {filePath}");
  254. return true;
  255. }
  256. // 否则查找版本化备份并恢复最新可用的
  257. var latestBackup = FindLatestVisionProBackup(filePath);
  258. if (latestBackup != null)
  259. {
  260. File.Copy(latestBackup, filePath, true);
  261. Log($"强制从版本备份恢复: {filePath}");
  262. return true;
  263. }
  264. return false;
  265. }
  266. catch (Exception ex)
  267. {
  268. Log($"强制恢复失败: {ex.Message}");
  269. return false;
  270. }
  271. }
  272. /// <summary>
  273. /// 尝试从已知备份恢复主文件(单参数便捷版本)。
  274. /// 会检查 .bak 与版本化备份并在找到有效备份时覆盖主文件。
  275. /// </summary>
  276. private static bool TryRestoreVisionProFile(string filePath)
  277. {
  278. try
  279. {
  280. string backupPath = filePath + ".bak";
  281. if (File.Exists(backupPath) && ValidateVisionProFile(backupPath))
  282. {
  283. File.Copy(backupPath, filePath, true);
  284. Log($"从备份恢复文件: {filePath}");
  285. return true;
  286. }
  287. // 若 .bak 不可用,尝试版本化备份
  288. var latestBackup = FindLatestVisionProBackup(filePath);
  289. if (latestBackup != null)
  290. {
  291. File.Copy(latestBackup, filePath, true);
  292. Log($"从版本备份恢复文件: {filePath}");
  293. return true;
  294. }
  295. return false;
  296. }
  297. catch (Exception ex)
  298. {
  299. Log($"恢复文件失败: {ex.Message}");
  300. return false;
  301. }
  302. }
  303. /// <summary>
  304. /// 批量检查并修复目录下所有 .vpp 文件。
  305. /// </summary>
  306. /// <param name="directoryPath">根目录路径</param>
  307. public static void CheckAndRepairDirectory(string directoryPath)
  308. {
  309. if (!Directory.Exists(directoryPath))
  310. return;
  311. // 查找所有 .vpp 文件(包含子目录)
  312. var vppFiles = Directory.GetFiles(directoryPath, "*.vpp", SearchOption.AllDirectories);
  313. Log($"开始检查和修复 {vppFiles.Length} 个VisionPro文件...");
  314. int repairedCount = 0;
  315. foreach (var file in vppFiles)
  316. {
  317. if (CheckAndRepairVisionProFile(file))
  318. {
  319. repairedCount++;
  320. }
  321. }
  322. Log($"完成检查和修复,共修复 {repairedCount} 个文件");
  323. }
  324. #endregion
  325. #region 私有实现方法
  326. /// <summary>
  327. /// 根据配置创建备份:
  328. /// - 若 KeepBackupFiles 为 true,则在指定 BackupDirectory 中创建带时间戳的版本备份并清理旧版本;
  329. /// - 若为 false,则不在此方法中处理 .bak(.bak 由 File.Replace 在保存时产生)。
  330. /// </summary>
  331. /// <param name="originalPath">原文件路径</param>
  332. private static void CreateVisionProBackup(string originalPath)
  333. {
  334. try
  335. {
  336. if (_options.KeepBackupFiles)
  337. {
  338. // 在原文件目录下创建 backupDir(如果不存在)
  339. string backupDir = Path.Combine(Path.GetDirectoryName(originalPath), _options.BackupDirectory);
  340. if (!Directory.Exists(backupDir))
  341. Directory.CreateDirectory(backupDir);
  342. string fileName = Path.GetFileNameWithoutExtension(originalPath);
  343. string extension = Path.GetExtension(originalPath);
  344. string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff");
  345. string backupName = $"{fileName}_{timestamp}{extension}";
  346. string backupPath = Path.Combine(backupDir, backupName);
  347. File.Copy(originalPath, backupPath, true);
  348. Log($"创建VisionPro版本备份: {backupPath}");
  349. // 清理超过 MaxBackupVersions 的旧备份
  350. CleanupOldVisionProBackups(backupDir, fileName);
  351. }
  352. }
  353. catch (Exception ex)
  354. {
  355. Log($"创建VisionPro备份失败: {ex.Message}");
  356. }
  357. }
  358. /// <summary>
  359. /// 生成一个用于临时保存的唯一文件路径(位于系统临时目录)。
  360. /// </summary>
  361. /// <param name="originalPath">与原文件关联的文件名(用于可读性)</param>
  362. /// <returns>临时文件路径</returns>
  363. private static string GetVisionProTempFilePath(string originalPath)
  364. {
  365. return originalPath + ".tmp";
  366. //string tempDir = Path.GetTempPath();
  367. //string safeName = Path.GetFileName(originalPath)
  368. // .Replace(" ", "_")
  369. // .Replace(":", "_");
  370. //return Path.Combine(tempDir, $"vp_{Guid.NewGuid():N}_{safeName}");
  371. }
  372. /// <summary>
  373. /// 尝试通过加载文件来验证 VisionPro 文件的完整性(加载失败视为无效)。
  374. /// </summary>
  375. /// <param name="filePath">要验证的文件路径</param>
  376. /// <returns>若能成功加载返回 true,否则 false</returns>
  377. private static bool ValidateVisionProFile(string filePath)
  378. {
  379. try
  380. {
  381. // 使用 Cognex 的序列化加载验证完整性
  382. var obj = CogSerializer.LoadObjectFromFile(filePath);
  383. return obj != null;
  384. }
  385. catch
  386. {
  387. return false;
  388. }
  389. }
  390. /// <summary>
  391. /// 尝试从 .bak 备份恢复并返回加载的对象(泛型)。
  392. /// 若恢复成功同时尝试将备份复制回主文件以保持一致性。
  393. /// </summary>
  394. private static T TryRecoverVisionProFile<T>(string originalPath) where T : class
  395. {
  396. string backupPath = originalPath + ".bak";
  397. if (File.Exists(backupPath))
  398. {
  399. try
  400. {
  401. var obj = CogSerializer.LoadObjectFromFile(backupPath) as T;
  402. if (obj != null)
  403. {
  404. // 当备份可用时,尝试恢复主文件
  405. TryRestoreVisionProFile(originalPath);
  406. Log($"从备份恢复VisionPro对象: {originalPath}");
  407. return obj;
  408. }
  409. }
  410. catch (Exception ex)
  411. {
  412. Log($"从备份恢复VisionPro对象失败: {ex.Message}");
  413. }
  414. }
  415. return default;
  416. }
  417. /// <summary>
  418. /// 查找版本化备份并尝试恢复为指定类型,成功时会将备份复制为主文件并返回对象。
  419. /// </summary>
  420. private static T TryFindAndRecoverVisionProFile<T>(string originalPath) where T : class
  421. {
  422. var backup = FindLatestVisionProBackup(originalPath);
  423. if (backup != null)
  424. {
  425. try
  426. {
  427. var obj = CogSerializer.LoadObjectFromFile(backup) as T;
  428. if (obj != null)
  429. {
  430. File.Copy(backup, originalPath, true);
  431. Log($"从版本备份恢复VisionPro对象: {originalPath}");
  432. return obj;
  433. }
  434. }
  435. catch (Exception ex)
  436. {
  437. Log($"从版本备份恢复VisionPro对象失败: {ex.Message}");
  438. }
  439. }
  440. return default;
  441. }
  442. /// <summary>
  443. /// 在备份目录和当前目录下查找与原文件对应的最新备份文件路径(按写入时间排序)。
  444. /// 支持多种命名模式以提高鲁棒性。
  445. /// </summary>
  446. private static string FindLatestVisionProBackup(string originalPath)
  447. {
  448. string directory = Path.GetDirectoryName(originalPath);
  449. string fileName = Path.GetFileNameWithoutExtension(originalPath);
  450. string extension = Path.GetExtension(originalPath);
  451. if (!Directory.Exists(directory))
  452. return null;
  453. // 优先检查专用备份目录
  454. string backupDir = Path.Combine(directory, _options.BackupDirectory);
  455. if (Directory.Exists(backupDir))
  456. {
  457. var backups = Directory.GetFiles(backupDir, $"{fileName}_*{extension}")
  458. .OrderByDescending(f => File.GetLastWriteTime(f))
  459. .ToList();
  460. if (backups.Any())
  461. return backups.First();
  462. }
  463. // 检查当前目录中常见的备份命名模式
  464. var backupPatterns = new[]
  465. {
  466. $"{fileName}.*.bak",
  467. $"{fileName}_*{extension}",
  468. $"{fileName}.backup.*"
  469. };
  470. foreach (var pattern in backupPatterns)
  471. {
  472. try
  473. {
  474. var backups = Directory.GetFiles(directory, pattern)
  475. .OrderByDescending(f => File.GetLastWriteTime(f))
  476. .ToList();
  477. if (backups.Any())
  478. return backups.First();
  479. }
  480. catch
  481. {
  482. continue;
  483. }
  484. }
  485. return null;
  486. }
  487. /// <summary>
  488. /// 恢复主文件的重载版本(从指定备份路径复制到原文件路径)。
  489. /// </summary>
  490. private static void TryRestoreVisionProFile(string backupPath, string originalPath)
  491. {
  492. try
  493. {
  494. File.Copy(backupPath, originalPath, true);
  495. Log($"恢复VisionPro主文件: {originalPath}");
  496. }
  497. catch (Exception ex)
  498. {
  499. Log($"恢复VisionPro主文件失败: {ex.Message}");
  500. }
  501. }
  502. /// <summary>
  503. /// 清理与指定原文件可能残留的各种临时文件(本进程或其他异常中断时产生)。
  504. /// 支持单个文件名与通配符模式(在系统临时目录下)。
  505. /// </summary>
  506. /// <param name="originalPath">原文件路径</param>
  507. private static void CleanVisionProTempFile(string originalPath)
  508. {
  509. // 可能的临时文件模式集合
  510. var tempPatterns = new[]
  511. {
  512. originalPath + ".vp_tmp",
  513. originalPath + ".tmp",
  514. Path.GetTempPath() + $"vp_*_{Path.GetFileName(originalPath)}"
  515. };
  516. foreach (var pattern in tempPatterns)
  517. {
  518. try
  519. {
  520. if (File.Exists(pattern))
  521. {
  522. File.Delete(pattern);
  523. Log($"清理VisionPro临时文件: {pattern}");
  524. }
  525. else if (pattern.Contains("*"))
  526. {
  527. // 通配符处理:删除系统临时目录下匹配的文件
  528. var files = Directory.GetFiles(Path.GetTempPath(), $"vp_*_{Path.GetFileName(originalPath)}");
  529. foreach (var file in files)
  530. {
  531. File.Delete(file);
  532. Log($"清理VisionPro临时文件: {file}");
  533. }
  534. }
  535. }
  536. catch (Exception ex)
  537. {
  538. Log($"清理VisionPro临时文件失败: {ex.Message}");
  539. }
  540. }
  541. }
  542. /// <summary>
  543. /// 尝试从临时文件恢复到主文件(当临时文件完整且主文件缺失或临时文件更可靠时)。
  544. /// 若临时文件无效则删除该临时文件。
  545. /// </summary>
  546. /// <param name="tempPath">临时文件路径(通常以 .vp_tmp 结尾)</param>
  547. private static void TryRecoverFromVisionProTemp(string tempPath)
  548. {
  549. try
  550. {
  551. string originalPath = tempPath.Replace(".vp_tmp", "");
  552. // 当主文件不存在或者临时文件验证通过时,尝试恢复
  553. if (!File.Exists(originalPath) || ValidateVisionProFile(tempPath))
  554. {
  555. if (ValidateVisionProFile(tempPath))
  556. {
  557. File.Move(tempPath, originalPath);
  558. Log($"从临时文件恢复VisionPro文件: {originalPath}");
  559. }
  560. else
  561. {
  562. File.Delete(tempPath);
  563. }
  564. }
  565. }
  566. catch
  567. {
  568. SafeDelete(tempPath);
  569. }
  570. }
  571. /// <summary>
  572. /// 清理备份目录中过多的旧版本,保留最新的 _options.MaxBackupVersions 个版本。
  573. /// </summary>
  574. private static void CleanupOldVisionProBackups(string backupDir, string baseName)
  575. {
  576. try
  577. {
  578. var files = Directory.GetFiles(backupDir)
  579. .Where(f => Path.GetFileName(f).StartsWith(baseName + "_"))
  580. .OrderByDescending(f => File.GetLastWriteTime(f))
  581. .ToList();
  582. for (int i = _options.MaxBackupVersions; i < files.Count; i++)
  583. {
  584. SafeDelete(files[i]);
  585. }
  586. }
  587. catch
  588. {
  589. // 忽略清理错误以避免影响主流程
  590. }
  591. }
  592. /// <summary>
  593. /// 安全删除文件(吞掉异常),用于清理临时/备份文件时保证不抛出影响主逻辑的异常。
  594. /// </summary>
  595. private static void SafeDelete(string filePath)
  596. {
  597. try
  598. {
  599. if (File.Exists(filePath))
  600. File.Delete(filePath);
  601. }
  602. catch
  603. {
  604. // 忽略删除错误
  605. }
  606. }
  607. /// <summary>
  608. /// 日志记录适配器(当前使用 LogHelper.WriteLogDebug)。
  609. /// 可根据项目替换为其它日志实现。
  610. /// </summary>
  611. /// <param name="message">日志消息</param>
  612. private static void Log(string message)
  613. {
  614. // 这里可以使用你喜欢的日志系统
  615. //Console.WriteLine($"[VisionProFileHelper] {DateTime.Now:HH:mm:ss} - {message}");
  616. LogHelper.WriteLogDebug($"[VisionProFileHelper] {DateTime.Now:HH:mm:ss} - {message}");
  617. // 或者使用:Debug.WriteLine(message);
  618. // 或者使用:Logger.Log(message);
  619. }
  620. #endregion
  621. }
  622. }