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