How to Load & Save to val's in app.config file?
How to Load & Save to values in app.config file ?
For example: to save & to 开发者_开发问答load Connection string
You use the ConfigurationManager class, which has a Save method. That documentation page has a comprehensive example of loading custom configuration sections and saving values.
Try this
public static string ConnectionString
{
get
{
try
{
return ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString;
}
catch
{
return "";
}
}
set
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["YourConnectionStringName "].ConnectionString = value;
config.Save(ConfigurationSaveMode.Minimal, true);
//refresh so that you can use the updates value directly without the need to restart the application
System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");
}
}
精彩评论