CacheFilePath.cs 4.5 KB

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