using ComPDFKit.PDFDocument; using PDF_Office.Properties; using Prism.Mvvm; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PDF_Office.Helper { /// /// 用于创建、获取、删除缓存文件的辅助类 /// 功能模块需要创建缓存文件夹时,统一在此类里处理 /// 临时文件在app启动时进行删除 /// public class CacheFilePath : BindableBase { private static readonly CacheFilePath instance = new CacheFilePath(); /// /// 图章缓存文件夹路径 /// List CustomStamp = new List { "CustomStamp" }; List SignatureFreeHand = new List { "Signature", "FreeHand" }; List SignatureStamp = new List { "Signature", "Stamp" }; List MergeFile = new List { "Temp" }; private CPDFDocument copyDoc; /// /// 页面编辑复制粘贴的临时缓存文件 /// public CPDFDocument CopyDoc { get { return copyDoc; } set { SetProperty(ref copyDoc, value); } } public static CacheFilePath Instance => instance; private CacheFilePath() { } ~CacheFilePath() { if (CopyDoc != null) { CopyDoc.Release(); } } /// /// 自定图章缓存文件夹 /// public string CustomStampPath { get { return CreateCacheDirectory(CustomStamp); } } /// /// 签名图章缓存文件夹 /// public string SignatureStampPath { get { return CreateCacheDirectory(SignatureStamp); } } /// /// 签名手绘缓存文件夹 /// public string SignatureFreeHandPath { get { return CreateCacheDirectory(SignatureFreeHand); } } /// /// 合并文件的缓存文件夹 /// public string MergeFilePath { get { return CreateCacheDirectory(MergeFile); } } /// /// 在“文档”路径下创建缓存文件夹,传C:\Users\kdan\Documents\PDF Office 以后的文件夹名 /// /// 文件路径列表,首位为第一个文件夹名,以此类推 /// private string CreateCacheDirectory(List directoryName) { try { string Path = App.CurrentPath; for (int i = 0; i < directoryName.Count; i++) { Path = System.IO.Path.Combine(Path, directoryName[i]); } System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(Path); if (directoryInfo.Exists && (directoryInfo.Attributes & System.IO.FileAttributes.ReadOnly) != System.IO.FileAttributes.ReadOnly && (directoryInfo.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden ) { return Path; } else { return ""; } } catch (Exception) { return ""; } } /// /// 将临时文件添加到待删除列表,app下次启动时删除 /// /// public void AddToDeleteFiles(string file) { //添加时不做是否存在判断,考虑可能每个人调用的顺序不一样,在删除时再做判断 try { if (!Settings.Default.AppProperties.NeedToDeletePath.Contains(file)) { Settings.Default.AppProperties.NeedToDeletePath.Add(file); } Settings.Default.Save(); //Save后,需要调用reload 防止互相占用文件,引起崩溃,具体效果待验证 Settings.Default.Reload(); } catch { } } public void AddToDeleteFiles(List files) { foreach (string file in files) { AddToDeleteFiles(file); } } /// /// 启动时删除临时文件 /// public void ClearDeleteFiles() { try { foreach (string file in Settings.Default.AppProperties.NeedToDeletePath) { if (File.Exists(file)) { File.Delete(file); } } Settings.Default.AppProperties.NeedToDeletePath.Clear(); //清除Temp文件夹及该目录中的所有子内容 string TempPath= Path.Combine(App.CurrentPath, "Temp"); DirectoryInfo tempfolder = new DirectoryInfo(TempPath); if (tempfolder.Exists) { Directory.Delete(TempPath, true); } } catch { } } } }