更改app.config配置文件的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改app.config配置文件的方法相关的知识,希望对你有一定的参考价值。

  

1.修改appSettings设置

     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var setUrl = config.AppSettings.Settings["ApiAddress"];
            if (setUrl==null)
            {
                config.AppSettings.Settings.Add("ApiAddress", ApiHelper.ApiUrl); ;
                config.Save(ConfigurationSaveMode.Modified);//保存
                ConfigurationManager.RefreshSection("appSettings");//刷新(防止已读入内存)
            }
            else if(setUrl.Value != ApiHelper.ApiUrl)
            {
                config.AppSettings.Settings["ApiAddress"].Value = ApiHelper.ApiUrl;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }

2.根据其他项目的appconfig修改当前项目的连接字符串

 /// <summary>
        /// 检测api项目的config是不是更改了 如果改了就更新当前项目的config
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <param name="newConnectionString"></param>
        private void UpdateConfigDBAddress()
        {
            try
            {
                var licenseConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string apiPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\Web.config");
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = apiPath;
                Configuration apiConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

                UpdateConfigDBAddress(licenseConfig, apiConfig, "connectionStringName");
            }
            catch
            { }
        }

        private void UpdateConfigDBAddress(Configuration licenseConfig,Configuration apiConfig,string connectionStringName)
        {
            ConnectionStringsSection apiSection = (ConnectionStringsSection)apiConfig.GetSection("connectionStrings");
            if (apiSection.ConnectionStrings[connectionStringName] != null)
            {
                string newValue = apiSection.ConnectionStrings[connectionStringName].ToString();
                ConnectionStringsSection licenseSection = (ConnectionStringsSection)licenseConfig.GetSection("connectionStrings");
                if (licenseSection.ConnectionStrings[connectionStringName] == null)
                {
                    ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(connectionStringName, newValue);
                    licenseSection.ConnectionStrings.Add(connectionStringSettings);
                    licenseConfig.Save(ConfigurationSaveMode.Full);
                }
                else if (newValue != licenseSection.ConnectionStrings[connectionStringName].ToString())
                {
                    licenseSection.ConnectionStrings[connectionStringName].ConnectionString = newValue;
                    licenseConfig.Save(ConfigurationSaveMode.Full);
                }
            }
        }

 

以上是关于更改app.config配置文件的方法的主要内容,如果未能解决你的问题,请参考以下文章

VB 2010 和 app.config 文件和配置文件已被另一个程序更改

关于App.config文件

配置文件(App.config文件)

如何在灌注时更改 App.config 文件?

怎样动态更改App.config中的配置

App.Config详解