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