.net 讀取自定義的appsetting配置文件


1.AppSettingExtension類的實現

    /// <summary>
    /// by lhc 2018.04.11
    /// 讀取自定義的appsetting配置文件 @"~/App_Data/AppSettingsConfig.xml"
    /// </summary>
    public class AppSettingExtension
    {
        private readonly static object lockObj = new object();

        /// <summary>
        /// 獲得配置信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="isWeb">是否web程序</param>
        /// <returns></returns>
        public static T GetValue<T>(string key, bool isWeb = true)
        {
            return GetValue(key, default(T), isWeb);
        }

        /// <summary>
        /// 獲取配置信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <param name="isWeb"></param>
        /// <returns></returns>
        public static T GetValue<T>(string key, T defaultValue, bool isWeb = true)
        {
            T value;
            try
            {
                Dictionary<string, string> appSettingsConfig = GetCustomAppSettingsDic(isWeb);
                if (appSettingsConfig.ContainsKey(key))
                {
                    value = (T)Convert.ChangeType(appSettingsConfig[key], typeof(T));
                }
                else
                {
                    value = defaultValue;
                }
            }
            catch
            {
                value = default(T);
            }
            return value;
        }

        /// <summary>
        /// 獲得配置信息列表
        /// </summary>
        /// <param name="isWeb"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetCustomAppSettingsDic(bool isWeb = true)
        {
            string AppSettingsConfigCacheKey = "AppName.AppSettingsConfig"; 
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (HttpRuntime.Cache.Get(AppSettingsConfigCacheKey) != null)
            {
                //讀取緩存
                dic = HttpRuntime.Cache.Get(AppSettingsConfigCacheKey) as Dictionary<string, string>;
            }
            else
            {
                //解析文件
                string filePath = string.Empty;
                if (isWeb)
                {
                    //web根目錄讀取配置文件
                    filePath = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, @"App_Data/AppSettingsConfig.xml");
                }
                else
                {
                    //執行文件根目錄讀取配置文件
                    filePath = System.IO.Path.Combine(System.Environment.CurrentDirectory, @"App_Data/AppSettingsConfig.xml");
                }
                //通用讀取根目錄
                //filePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,@"App_Data/AppSetingsConfig.xml");
                if (File.Exists(filePath))
                {
                    lock (lockObj)
                    {
                        XDocument doc;
                        using (FileStream fsIn = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                        {
                            doc = XDocument.Load(fsIn);
                        }
                        foreach (XElement element in doc.Element("appSettings").Descendants("add"))
                        {
                            string key = element.Attribute("key").Value;
                            string value = element.Attribute("value").Value;
                            if (!string.IsNullOrEmpty(key) && !dic.ContainsKey(key))
                            {
                                dic.Add(key, value);
                            }
                        }
                        //寫緩存
                        CacheDependency dp = new CacheDependency(filePath);//建立緩存依賴項
                        HttpRuntime.Cache.Insert(AppSettingsConfigCacheKey, dic, dp);
                    }
                }
            }
            return dic;
        }
    }

2.配置文件示例(@"~/App_Data/AppSettingsConfig.xml")

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="Version" value="1.0.0" />
<add key="Country" value="China"/>
</appSettings>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM