更新在运行时的app.config system.net设置 [英] Update app.config system.net setting at runtime

查看:392
本文介绍了更新在运行时的app.config system.net设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新在运行时的.Net EXE app.config文件的system.net SectionGroup的设置。我没有写访问在运行时的原始配置文件(我开发一个.net DLL加载其托管在由应用程序,我已经无法控制提供了一个exe),所以我希望能保存一份副本文件和替换在运行时修改后的版本的exe文件的配置。我已经试过以下,但它不工作。有什么建议?

 配置配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    NetSectionGroup netSectionGroup = config.GetSectionGroup(system.net)作为NetSectionGroup;
    netSectionGroup.Settings.HttpWebRequest.UseUnsafeHeaderParsing = TRUE;
    config.SaveAs(@C:\ ProgramData \ test.config,ConfigurationSaveMode.Full);

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

解决方案

我没有从你的问题理解,如果你没有因为自己的设计实现访问app.config文件,或者你没能保存配置文件,所以这里是一块code,使您可以修改和保存的appSettings节在运行时配置文件:

 配置配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection设置= config.AppSettings.Settings;

//更新SaveBeforeExit
。设置[-keyname  - ]值=newkeyvalue;
...
//保存文件
config.Save(ConfigurationSaveMode.Modified);
// relaod你修改的部分
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
 

PS的code也救不了你的解决方案编辑器中看到的app.config文件,它会PDATE的program_name.exe.config文件中的操作forlder。

I need to update a setting in the system.net SectionGroup of a .Net exe app.config file at runtime. I don't have write access to the original config file at runtime (I am developing a .Net dll add-in which is hosted in an exe provided by the app which I have no control over) so I was hoping to save a copy of the file and replace the config in the exe with the modified version at runtime. I've tried the following but it's not working. Any suggestions?

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

解决方案

I did not understand from your question if you don't have access to the app.config file because of your own design implementation or you just weren't able to save the config file, so here is a piece of code that allows you to modify and save appSettings section in the config file at runtime:

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 the code will not save the app.config file you see in the solution editor, it will pdate the "program_name.exe.config" file in the operation forlder.

这篇关于更新在运行时的app.config system.net设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆