|
@@ -0,0 +1,102 @@
|
|
|
|
+/*-------------------------------------------------------------------------
|
|
|
|
+ * 功能描述:ConfigurationExtension
|
|
|
|
+ * 作者:xulisong
|
|
|
|
+ * 创建时间: 2019/4/19 16:05:25
|
|
|
|
+ * 版本号:v1.0
|
|
|
|
+ * -------------------------------------------------------------------------*/
|
|
|
|
+
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Configuration;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Reflection;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+
|
|
|
|
+namespace SAGA.DotNetUtils.Extend
|
|
|
|
+{
|
|
|
|
+ public static class ConfigurationUtil
|
|
|
|
+ {
|
|
|
|
+ private static string GetDefaultPath()
|
|
|
|
+ {
|
|
|
|
+ string defaultPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BaseSettings.config");
|
|
|
|
+ return defaultPath;
|
|
|
|
+ }
|
|
|
|
+ private static Configuration m_Default;
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 默认的配置文件
|
|
|
|
+ /// </summary>
|
|
|
|
+ public static Configuration Default
|
|
|
|
+ {
|
|
|
|
+ get
|
|
|
|
+ {
|
|
|
|
+ if (m_Default == null)
|
|
|
|
+ {
|
|
|
|
+ m_Default = m_InitConfig?.Invoke();
|
|
|
|
+ }
|
|
|
|
+ if (m_Default == null)
|
|
|
|
+ {
|
|
|
|
+ var defaultPath = GetDefaultPath();
|
|
|
|
+ m_Default = CreateConfig(GetDefaultPath());
|
|
|
|
+ }
|
|
|
|
+ return m_Default;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private static Func<Configuration> m_InitConfig;
|
|
|
|
+ public static void Init(Func<Configuration> initConfig)
|
|
|
|
+ {
|
|
|
|
+ m_Default = null;
|
|
|
|
+ m_InitConfig = initConfig;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 初始化默认配置
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="initConfig"></param>
|
|
|
|
+ public static void Init(Configuration initConfig)
|
|
|
|
+ {
|
|
|
|
+ m_Default = initConfig;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 将指定路径配置文件,放到工具默认读取位置
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="path"></param>
|
|
|
|
+ public static bool SaveToDefault(string path)
|
|
|
|
+ {
|
|
|
|
+ if (!File.Exists(path))
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ File.Copy(path, GetDefaultPath(),true);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ public static string GetSettingValue(this Configuration config,string appsettingKey)
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (string.IsNullOrWhiteSpace(appsettingKey))
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return config.AppSettings.Settings[appsettingKey].Value;
|
|
|
|
+ }
|
|
|
|
+ catch (Exception)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public static Configuration CreateConfig(string configPath, ConfigurationUserLevel level)
|
|
|
|
+ {
|
|
|
|
+ ExeConfigurationFileMap map = new ExeConfigurationFileMap();
|
|
|
|
+ map.ExeConfigFilename = configPath; ;
|
|
|
|
+ Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, level);
|
|
|
|
+ return config;
|
|
|
|
+ }
|
|
|
|
+ public static Configuration CreateConfig(string configPath)
|
|
|
|
+ {
|
|
|
|
+ return CreateConfig(configPath, ConfigurationUserLevel.None);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|