zoukankan      html  css  js  c++  java
  • 更改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);
                    }
                }
            }

  • 相关阅读:
    PUTTY与SecureCRT的比较
    java中volatile关键字的含义
    java中引用的原理
    Java陷阱之assert关键字
    脏读 幻读 不可重复读
    JAVA 的wait(), notify()与synchronized同步机制
    线程状态转换图
    并行、并发、同步和互斥
    B-树学习笔记
    平衡二叉树及其应用场景
  • 原文地址:https://www.cnblogs.com/kgadb/p/6523550.html
Copyright © 2011-2022 走看看