c# 根據配置文件路徑,設置和獲取config文件 appSettings 節點值

一:獲取和設置默認配置文件
/// <summary>
        /// 獲取編譯後的主配置文件節點值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionStringsConfig(string key)
        {
            try
            {
                ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"DMSystem.exe.config" };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                return configuration.AppSettings.Settings[key].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 設置編譯後的主配置文件節點值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static void SetConnectionStringsConfig(string key, string value)
        {
            try
            {
                ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"DMSystem.exe.config" };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

                bool isModified = false;
                //檢測是否已經存在該鍵
                foreach (string tempKey in configuration.AppSettings.Settings.AllKeys)
                {
                    if (tempKey == key)
                    {
                        isModified = true;
                    }
                }
                if (isModified)
                {
                    configuration.AppSettings.Settings.Remove(key);
                }

                configuration.AppSettings.Settings.Add(key, value);
                configuration.Save(ConfigurationSaveMode.Modified);
                //刷新
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


 
2、獲取和設置指定配置文件/// <summary>
        /// 獲取指定config文件中appSettings節點下key的value
        /// </summary>
        /// <param name="key">appSettings > add > key </param>
        /// <param name="configPath">配置文件絕對路徑</param>
        /// <returns></returns>
        public static string GetAppSettings(string key, string configPath)
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
            var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            var strObj = configuration.AppSettings.Settings[key]; if (strObj == null)
            {
                throw new ArgumentException($"當前配置文件路徑{configuration.FilePath}\n文件中appSettings節點下未配置名爲:【{key}】的內容,請添加後再試");
            }
            return strObj.Value;
        }

        /// <summary>
        /// 修改指定config文件中appSettings節點下key的value
        /// </summary>
        /// <param name="name">appSettings > add > key </param>
        /// <param name="configPath">配置文件絕對路徑</param>
        /// <returns></returns>
        public static bool SetAppSettings(string key, string value, string configPath)
        {
            try
            {
                ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

                if (configuration.AppSettings.Settings[key] != null)
                    configuration.AppSettings.Settings[key].Value = value;
                else
                    configuration.AppSettings.Settings.Add(key, value);
                configuration.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch
            {
                return false;
            }
        }
相關文章
相關標籤/搜索