CacheFilePath.cs 5.6 KB

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