Browse Source

xls:配置文件修改

xulisong 6 years ago
parent
commit
61a3bd72fd

+ 38 - 1
MBI/SAGA.DotNetUtils/Extend/ConfigurationUtil.cs

@@ -13,16 +13,52 @@ using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using SAGA.DotNetUtils.Utilities;
 
 namespace SAGA.DotNetUtils.Extend
 {
     public static class ConfigurationUtil
     {
+        #region 默认路径持久化保存,应对向revit这种,启动之后设置,然后又跳转到其他程序的情况
+        private static string GetDefuatPathLocation()
+        {
+            return CommonUtil.GetPath(typeof(ConfigurationUtil), ".txt");
+        }
+        private static bool SavePersistenceDefuatPath(string defualtPath)
+        {
+            var path = GetDefuatPathLocation();
+            if (!string.IsNullOrWhiteSpace(path))
+            {
+                var directory = Directory.GetParent(path);
+                if (!directory.Exists)
+                {
+                    directory.Create();
+                }
+                File.WriteAllText(path, defualtPath);
+                return true;
+            }
+            return false;
+        }
+        private static string GetPersistencetDefuatPath()
+        {
+            var path = GetDefuatPathLocation();
+            if (!CommonUtil.InvalidFilePath(path))
+            {
+                return File.ReadAllText(path); ;
+            }
+            return null;
+        }
+        #endregion
+
         private static string m_DefaultPath;
         private static string GetDefaultPath()
         {
             var defualtPath = m_DefaultPath;
-            if (string.IsNullOrWhiteSpace(defualtPath) || !File.Exists(defualtPath))
+            if (CommonUtil.InvalidFilePath(defualtPath))
+            {
+                defualtPath = GetPersistencetDefuatPath();
+            }
+            if (CommonUtil.InvalidFilePath(defualtPath))
             {
                 defualtPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BaseSettings.config");
             }            
@@ -83,6 +119,7 @@ namespace SAGA.DotNetUtils.Extend
                 return false;
             }
             m_DefaultPath = path;
+            SavePersistenceDefuatPath(path);
             return true;
         }
         public static string GetSettingValue(this Configuration config,string appsettingKey)

+ 6 - 0
MBI/SAGA.DotNetUtils/Utilities/CommonUtil.cs

@@ -9,6 +9,7 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -31,5 +32,10 @@ namespace SAGA.DotNetUtils.Utilities
             string fileName = type.Name + extensionName;
             return Path.Combine(basePath, md5, fileName);
         }
+
+        public static bool InvalidFilePath(string filePath)
+        {
+            return string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath);
+        }
     }
 }