【问题标题】:Update app.config system.net setting at runtime在运行时更新 app.config system.net 设置
【发布时间】:2010-11-02 02:24:12
【问题描述】:

我需要在运行时更新 .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");

【问题讨论】:

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


    【解决方案1】:

    如果您因为自己的设计实现而无法访问 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”文件。

    【讨论】:

    • 我正在为 .Net 应用程序 (Windows Media Center) 开发插件。该插件采用 dll .Net 程序集的形式,该程序集加载到由 Media Center exe (ehshell.exe) 管理的单独主机进程 (ehexthost.exe) 中。我无法保存到主机进程的原始 app.config 文件,因为它的位置,因此需要保存到备用路径。此外,我想更改 system.net SectionGroup 而不是 AppSettings。谢谢。
    • 我可以确认这也适用于 .NET 4.0。(感谢@AlexDrenea!)对我来说,关键(请原谅双关语)是这样的设置:<configuration> <appSettings> <add key="blahblah" value="bloog" /> </appSettings> </configuration>
    • 已确认... 在调试模式下不会保存。只有运行 bin 文件夹中的 .exe 才会保存。我没有看到它保存在“program_name.exe.config”文件中的任何证据。
    【解决方案2】:

    使用 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);
    
    
        }
    

    【讨论】:

      【解决方案3】:

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

      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);
      

      【讨论】:

        猜你喜欢
        • 2011-11-06
        • 1970-01-01
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 2010-12-05
        相关资源
        最近更新 更多