CacheFilePath.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.Properties;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace PDF_Master.Helper
  11. {
  12. /// <summary>
  13. /// 用于创建、获取、删除缓存文件的辅助类
  14. /// 功能模块需要创建缓存文件夹时,统一在此类里处理
  15. /// 临时文件在app启动时进行删除
  16. /// </summary>
  17. public class CacheFilePath : BindableBase
  18. {
  19. private static readonly CacheFilePath instance = new CacheFilePath();
  20. /// <summary>
  21. /// 图章缓存文件夹路径
  22. /// </summary>
  23. List<string> CustomStamp = new List<string> { "CustomStamp" };
  24. List<string> SignatureFreeHand = new List<string> { "Signature", "FreeHand" };
  25. List<string> SignatureStamp = new List<string> { "Signature", "Stamp" };
  26. List<string> MergeFile = new List<string> { "Temp" };
  27. /// <summary>
  28. /// 扫描仪缓存文件夹路径
  29. /// </summary>
  30. List<string> ScanFile = new List<string> { "ScanTemp" };
  31. private CPDFDocument copyDoc;
  32. /// <summary>
  33. /// 页面编辑复制粘贴的临时缓存文件
  34. /// </summary>
  35. public CPDFDocument CopyDoc
  36. {
  37. get { return copyDoc; }
  38. set
  39. {
  40. SetProperty(ref copyDoc, value);
  41. }
  42. }
  43. public static CacheFilePath Instance => instance;
  44. private CacheFilePath()
  45. {
  46. }
  47. ~CacheFilePath()
  48. {
  49. if (CopyDoc != null)
  50. {
  51. CopyDoc.Release();
  52. }
  53. }
  54. /// <summary>
  55. /// 自定图章缓存文件夹
  56. /// </summary>
  57. public string CustomStampPath
  58. {
  59. get
  60. {
  61. return CreateCacheDirectory(CustomStamp);
  62. }
  63. }
  64. /// <summary>
  65. /// 签名图章缓存文件夹
  66. /// </summary>
  67. public string SignatureStampPath
  68. {
  69. get
  70. {
  71. return CreateCacheDirectory(SignatureStamp);
  72. }
  73. }
  74. /// <summary>
  75. /// 签名手绘缓存文件夹
  76. /// </summary>
  77. public string SignatureFreeHandPath
  78. {
  79. get
  80. {
  81. return CreateCacheDirectory(SignatureFreeHand);
  82. }
  83. }
  84. /// <summary>
  85. /// 合并文件的缓存文件夹
  86. /// </summary>
  87. public string MergeFilePath
  88. {
  89. get
  90. {
  91. return CreateCacheDirectory(MergeFile);
  92. }
  93. }
  94. /// <summary>
  95. /// 扫描仪文件的缓存文件夹
  96. /// </summary>
  97. public string ScanFilePath
  98. {
  99. get
  100. {
  101. return CreateCacheDirectory(ScanFile);
  102. }
  103. }
  104. /// <summary>
  105. /// 在“文档”路径下创建缓存文件夹,传C:\Users\kdan\Documents\PDF Master 以后的文件夹名
  106. /// </summary>
  107. /// <param name="directoryName">文件路径列表,首位为第一个文件夹名,以此类推</param>
  108. /// <returns></returns>
  109. private string CreateCacheDirectory(List<string> directoryName)
  110. {
  111. try
  112. {
  113. string Path = App.CurrentPath;
  114. for (int i = 0; i < directoryName.Count; i++)
  115. {
  116. Path = System.IO.Path.Combine(Path, directoryName[i]);
  117. }
  118. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(Path);
  119. if (directoryInfo.Exists
  120. && (directoryInfo.Attributes & System.IO.FileAttributes.ReadOnly) != System.IO.FileAttributes.ReadOnly
  121. && (directoryInfo.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden
  122. )
  123. {
  124. return Path;
  125. }
  126. else
  127. {
  128. return "";
  129. }
  130. }
  131. catch (Exception)
  132. {
  133. return "";
  134. }
  135. }
  136. /// <summary>
  137. /// 将临时文件添加到待删除列表,app下次启动时删除
  138. /// </summary>
  139. /// <param name="file"></param>
  140. public void AddToDeleteFiles(string file)
  141. {
  142. //添加时不做是否存在判断,考虑可能每个人调用的顺序不一样,在删除时再做判断
  143. try
  144. {
  145. if (!Settings.Default.AppProperties.NeedToDeletePath.Contains(file))
  146. {
  147. Settings.Default.AppProperties.NeedToDeletePath.Add(file);
  148. }
  149. Settings.Default.Save();
  150. //Save后,需要调用reload 防止互相占用文件,引起崩溃,具体效果待验证
  151. Settings.Default.Reload();
  152. }
  153. catch { }
  154. }
  155. public void AddToDeleteFiles(List<string> files)
  156. {
  157. foreach (string file in files)
  158. {
  159. AddToDeleteFiles(file);
  160. }
  161. }
  162. /// <summary>
  163. /// 启动时删除临时文件
  164. /// </summary>
  165. public void ClearDeleteFiles()
  166. {
  167. try
  168. {
  169. foreach (string file in Settings.Default.AppProperties.NeedToDeletePath)
  170. {
  171. if (File.Exists(file))
  172. {
  173. File.Delete(file);
  174. }
  175. }
  176. Settings.Default.AppProperties.NeedToDeletePath.Clear();
  177. //清除ScanTemp文件夹及该目录中的所有子内容
  178. string ScanTempPath = Path.Combine(App.CurrentPath, "ScanTemp");
  179. DirectoryInfo Scantempfolder = new DirectoryInfo(ScanTempPath);
  180. if (Scantempfolder.Exists)
  181. {
  182. Directory.Delete(ScanTempPath, true);
  183. }
  184. //清除Temp文件夹及该目录中的所有子内容
  185. string TempPath= Path.Combine(App.CurrentPath, "Temp");
  186. DirectoryInfo tempfolder = new DirectoryInfo(TempPath);
  187. if (tempfolder.Exists)
  188. {
  189. Directory.Delete(TempPath, true);
  190. }
  191. }
  192. catch { }
  193. }
  194. }
  195. }