CacheFilePath.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using PDF_Office.Properties;
  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 PDF_Office.Helper
  9. {
  10. /// <summary>
  11. /// 用于创建、获取、删除缓存文件的辅助类
  12. /// 功能模块需要创建缓存文件夹时,统一在此类里处理
  13. /// 临时文件在app启动时进行删除
  14. /// </summary>
  15. public class CacheFilePath
  16. {
  17. private static readonly CacheFilePath instance = new CacheFilePath();
  18. public static CacheFilePath Instance => instance;
  19. private CacheFilePath()
  20. {
  21. }
  22. /// <summary>
  23. /// 自定图章缓存文件夹
  24. /// </summary>
  25. public string CustomStampPath
  26. {
  27. get
  28. {
  29. return CreateCacheDirectory("CustomStamp");
  30. }
  31. }
  32. /// <summary>
  33. /// 在“文档”路径下创建缓存文件夹,传C:\Users\kdan\Documents\PDF Office 以后的文件夹名
  34. /// </summary>
  35. /// <returns></returns>
  36. private string CreateCacheDirectory(string directoryName)
  37. {
  38. try
  39. {
  40. string Path = System.IO.Path.Combine(App.CurrentPath, directoryName);
  41. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(Path);
  42. if (directoryInfo.Exists
  43. && (directoryInfo.Attributes & System.IO.FileAttributes.ReadOnly) != System.IO.FileAttributes.ReadOnly
  44. && (directoryInfo.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden
  45. )
  46. {
  47. return Path;
  48. }
  49. else
  50. {
  51. return "";
  52. }
  53. }
  54. catch (Exception)
  55. {
  56. return "";
  57. }
  58. }
  59. /// <summary>
  60. /// 将临时文件添加到待删除列表,app下次启动时删除
  61. /// </summary>
  62. /// <param name="file"></param>
  63. public void AddToDeleteFiles(string file)
  64. {
  65. //添加时不做是否存在判断,考虑可能每个人调用的顺序不一样,在删除时再做判断
  66. try
  67. {
  68. if (!Settings.Default.AppProperties.NeedToDeletePath.Contains(file))
  69. {
  70. Settings.Default.AppProperties.NeedToDeletePath.Add(file);
  71. }
  72. Settings.Default.Save();
  73. //Save后,需要调用reload 防止互相占用文件,引起崩溃,具体效果待验证
  74. Settings.Default.Reload();
  75. }
  76. catch { }
  77. }
  78. public void AddToDeleteFiles(List<string> files)
  79. {
  80. foreach(string file in files)
  81. {
  82. AddToDeleteFiles(file);
  83. }
  84. }
  85. /// <summary>
  86. /// 启动时删除临时文件
  87. /// </summary>
  88. public void ClearDeleteFiles()
  89. {
  90. try
  91. {
  92. foreach (string file in Settings.Default.AppProperties.NeedToDeletePath)
  93. {
  94. if (File.Exists(file))
  95. {
  96. File.Delete(file);
  97. }
  98. }
  99. Settings.Default.AppProperties.NeedToDeletePath.Clear();
  100. }
  101. catch { }
  102. }
  103. }
  104. }