mth*_*nal 23 .net c# system.net app-config
我需要在运行时更新.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");
Run Code Online (Sandbox Code Playgroud)
Ale*_*nea 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);
Run Code Online (Sandbox Code Playgroud)
PS代码不会保存您在解决方案编辑器中看到的app.config文件,它将在操作forlder中修改"program_name.exe.config"文件.
| 归档时间: |
|
| 查看次数: |
36247 次 |
| 最近记录: |