Can someone give me an example of how to save a key/value in app.config using C#
and WinForms?
有人能举例说明如何使用C#和WinForms在app.config中保存键/值吗?
3 个解决方案
#1
In ASP.NET:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
In WinForms:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
#2
I know you specifically asked for WinForms solution, but this might help some others. For a .NET 4.0 console application, none of these worked for me. So I used the following and it worked:
我知道你特意要求WinForms解决方案,但这可能会帮助其他人。对于.NET 4.0控制台应用程序,这些都不适用于我。所以我使用了以下内容并且它有效:
private static void UpdateSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
#3
Something like this may be : http://geekswithblogs.net/akraus1/articles/64871.aspx
这样的事情可能是:http://geekswithblogs.net/akraus1/articles/64871.aspx
#1
In ASP.NET:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
In WinForms:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);
#2
I know you specifically asked for WinForms solution, but this might help some others. For a .NET 4.0 console application, none of these worked for me. So I used the following and it worked:
我知道你特意要求WinForms解决方案,但这可能会帮助其他人。对于.NET 4.0控制台应用程序,这些都不适用于我。所以我使用了以下内容并且它有效:
private static void UpdateSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
#3
Something like this may be : http://geekswithblogs.net/akraus1/articles/64871.aspx
这样的事情可能是:http://geekswithblogs.net/akraus1/articles/64871.aspx