CacheFilePath.cs 3.8 KB

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