在运行时更新app.config system.net设置

时间:2009-06-11 10:44:30

标签: c# .net app-config system.net

我需要在运行时更新.Net exe app.config文件的system.net SectionGroup中的设置。我没有在运行时对原始配置文件的写访问权限(我正在开发一个.Net dll加载项,该加载项托管在我无法控制的应用程序提供的exe中)所以我希望保存一份副本的文件,并在运行时用修改后的版本替换exe中的配置。我尝试过以下但是没有用。有什么建议吗?

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    NetSectionGroup netSectionGroup = config.GetSectionGroup("system.net") as NetSectionGroup;
    netSectionGroup.Settings.HttpWebRequest.UseUnsafeHeaderParsing = true;                      
    config.SaveAs(@"C:\ProgramData\test.config", ConfigurationSaveMode.Full);

    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\ProgramData\test.config");

3 个答案:

答案 0 :(得分:45)

如果您因为自己的设计实现无法访问app.config文件或者您无法保存配置文件,我从您的问题中无法理解,所以这里有一段代码允许您在运行时修改和保存配置文件中的appSettings部分:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;

// update SaveBeforeExit
settings[-keyname-].Value = "newkeyvalue";
...
//save the file
config.Save(ConfigurationSaveMode.Modified);
//relaod the section you modified
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

P.S代码不会保存您在解决方案编辑器中看到的app.config文件,它将在操作forlder中修改“program_name.exe.config”文件。

答案 1 :(得分:4)

使用System.Configuration;

    public void save_new_connection()
    {

      string ConStrng = ConfigurationManager.ConnectionStrings.ToString();
      ConnectionStringSettings conSetting = new ConnectionStringSettings();

      conSetting.ConnectionString="server=localho;UserId=root;password=mypass;database=night_anglecourier"; 
      conSetting.Name = "courier.Properties.Settings.night_anglecourierConnectionString";
      conSetting.ProviderName = "MySql.Data.MySqlClient";

      System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ConnectionStringsSection conSettings = (ConnectionStringsSection)config.GetSection("connectionStrings");
        conSettings.ConnectionStrings.Remove(conSetting);
        conSettings.ConnectionStrings.Add(conSetting);

        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);


    }

答案 2 :(得分:2)

使用此代码我已经更改了配置文件的应用程序设置中的连接字符串...希望这可能有助于你。

string ConStrng = ConfigurationSettings.AppSettings["ConnectionString"];
            string sss = "Data Source=";
            string xxx = ";Initial Catalog=AlfalahScholarship;Integrated Security=True";
            //ConfigurationSettings.AppSetting;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //Get the appSettings section.
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            appSettings.Settings.Remove("ConnectionString");
            appSettings.Settings.Add("ConnectionString", sss + txtServerName.Text + xxx);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);