CacheFilePath.cs 7.8 KB

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