Ver Fonte

其他-补充缓存文件辅助类

ZhouJieSheng há 2 anos atrás
pai
commit
1fd69cc245
2 ficheiros alterados com 113 adições e 33 exclusões
  1. 28 15
      PDF Office/App.xaml.cs
  2. 85 18
      PDF Office/Helper/CacheFilePath.cs

+ 28 - 15
PDF Office/App.xaml.cs

@@ -68,15 +68,42 @@ namespace PDF_Office
         var countryCode = RegionInfo.CurrentRegion.TwoLetterISORegionName;//上传国家信息
         AppCenter.SetCountryCode(countryCode);
 #endif
+        }
+
+        private void InitSettings()
+        {
+            if (Settings.Default.RecentOpenFiles == null)
+                Settings.Default.RecentOpenFiles = new RecentOpenFiles();
+
+            if (Settings.Default.AppProperties == null)
+                Settings.Default.AppProperties = new PDFSettings.APPSettingProperties();
+
+            if (Settings.Default.AllPDFToolsList == null)
+                Settings.Default.AllPDFToolsList = new PDFSettings.AllPDFToolsList();
+
+            if (Settings.Default.QuickPDFToolsList == null)
+                Settings.Default.QuickPDFToolsList = new PDFSettings.QuickPDFToolsList();
+
+        }
+
+        protected override void OnStartup(StartupEventArgs e)
+        {
             LicenseVerify();
 
             InitSettings();
 
+
+            //清除临时文件
+            CacheFilePath.Instance.ClearDeleteFiles();
+
             try
             {
                 DirectoryInfo info = new DirectoryInfo(CurrentPath);
+                //避免因存在同名文件,导致无法创建文件夹
                 if (File.Exists(CurrentPath))
+                {
                     File.Delete(CurrentPath);
+                }
                 if (!info.Exists)
                 {
                     info.Create();
@@ -85,22 +112,8 @@ namespace PDF_Office
             catch (Exception ex)
             {
             }
-        }
-
-        private void InitSettings()
-        {
-            if (Settings.Default.RecentOpenFiles == null)
-                Settings.Default.RecentOpenFiles = new RecentOpenFiles();
-
-            if (Settings.Default.AppProperties == null)
-                Settings.Default.AppProperties = new PDFSettings.APPSettingProperties();
-
-            if (Settings.Default.AllPDFToolsList == null)
-                Settings.Default.AllPDFToolsList = new PDFSettings.AllPDFToolsList();
-
-            if (Settings.Default.QuickPDFToolsList == null)
-                Settings.Default.QuickPDFToolsList = new PDFSettings.QuickPDFToolsList();
 
+            base.OnStartup(e);
         }
 
         /// <summary>

+ 85 - 18
PDF Office/Helper/CacheFilePath.cs

@@ -1,46 +1,113 @@
-using System;
+using PDF_Office.Properties;
+using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 namespace PDF_Office.Helper
 {
-    class CacheFilePath
+    /// <summary>
+    /// 用于创建、获取、删除缓存文件的辅助类
+    /// 功能模块需要创建缓存文件夹时,统一在此类里处理
+    /// 临时文件在app启动时进行删除
+    /// </summary>
+    public class CacheFilePath
     {
         private static readonly CacheFilePath instance = new CacheFilePath();
+
+        public static CacheFilePath Instance => instance;
+
         private CacheFilePath()
         {
 
         }
-        public static CacheFilePath Instance => instance;
 
+        /// <summary>
+        /// 自定图章缓存文件夹
+        /// </summary>
         public string CustomStampPath
         {
             get
             {
-                try
+               return CreateCacheDirectory("CustomStamp");
+            }
+        }
+
+        /// <summary>
+        /// 在“文档”路径下创建缓存文件夹,传C:\Users\kdan\Documents\PDF Office 以后的文件夹名
+        /// </summary>
+        /// <returns></returns>
+        private string CreateCacheDirectory(string directoryName)
+        {
+            try
+            {
+                string Path = System.IO.Path.Combine(App.CurrentPath, directoryName);
+                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
+                    )
                 {
-                    string Path = System.IO.Path.Combine(App.CurrentPath, "CustomStamp");
-                    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 "";
-                    }
+                    return Path;
                 }
-                catch (Exception)
+                else
                 {
                     return "";
                 }
+            }
+            catch (Exception)
+            {
+                return "";
+            }
+        }
 
+        /// <summary>
+        /// 将临时文件添加到待删除列表,app下次启动时删除
+        /// </summary>
+        /// <param name="file"></param>
+        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<string> files)
+        {
+            foreach(string file in files)
+            {
+                AddToDeleteFiles(file);
+            }
+        }
+
+        /// <summary>
+        /// 启动时删除临时文件
+        /// </summary>
+        public void ClearDeleteFiles()
+        {
+            try
+            {
+                foreach (string file in Settings.Default.AppProperties.NeedToDeletePath)
+                {
+                    if (File.Exists(file))
+                    {
+                        File.Delete(file);
+                    }
+                }
+                Settings.Default.AppProperties.NeedToDeletePath.Clear();
             }
+            catch { }
         }
 
     }