更改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);
                }
            }
        }

 

posted on 2017-03-08 23:04  狂狗A地板  阅读(416)  评论(1编辑  收藏  举报

导航