CacheFilePath.cs 7.1 KB

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