CacheFilePath.cs 5.2 KB

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