CacheFilePath.cs 7.5 KB

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