C# 在运行时更新 app.config system.net 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/980440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update app.config system.net setting at runtime
提问by mthornal
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?
我需要在运行时更新 .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");
回答by AlexDrenea
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:
我不明白你的问题是因为你自己的设计实现而无法访问 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 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.
PS代码不会保存您在解决方案编辑器中看到的app.config文件,它会更新操作文件夹中的“program_name.exe.config”文件。
回答by Hmansoor
with this code i have changed the connection string in the application setting of the config file ... hope this may help u.
使用此代码,我更改了配置文件的应用程序设置中的连接字符串...希望这可以帮助您。
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);
回答by Dileepa Ruwan Dissanayake
using System.Configuration;
使用 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);
}