ConfigurationManager.AppSettings – 如何修改和保存?

这听起来可能听起来太琐碎,我也是按照文章中的build议做同样的事情,但是它并没有像预期的那样工作。 希望有人能指点我正确的方向。

我想保存每个AppSettings的用户设置。

一旦Winformclosures,我触发这个:

conf.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (ConfigurationManager.AppSettings["IntegrateWithPerforce"] != null) ConfigurationManager.AppSettings["IntegrateWithPerforce"] = e.Payload.IntegrateCheckBox.ToString(); else config.AppSettings.Settings.Add("IntegrateWithPerforce", e.Payload.IntegrateCheckBox.ToString()); config.Save(ConfigurationSaveMode.Modified); 

所以当第一次入口不存在的时候,它只会创build它,否则会修改现有的入口。 但是这并不能保存。

1)我做错了什么?

2)我在哪里等待App设置的用户设置再次保存? 它在Debug文件夹中还是在C:\ Documents and Settings \ USERNAME \ Local Settings \ Application Data文件夹中?

也许你应该看看添加一个设置文件。 (如App.Settings)创build这个文件将允许你做到以下几点:

 string mysetting = App.Default.MySetting; App.Default.MySetting = "my new setting"; 

这意味着您可以编辑,然后更改项目,其中的项目是强types的,最重要的是,您不必在部署之前触摸任何xml!

结果是应用程序或用户上下文设置。

查看设置文件的“添加新项目”菜单。

关于如何更改app.config文件中appSettings部分的值:

 config.AppSettings.Settings.Remove(key); config.AppSettings.Settings.Add(key, value); 

做这个工作。

当然更好的做法是设置类,但这取决于你以后是什么。

我知道我迟到了:)但是,这是如何做到这一点:

 public static void AddOrUpdateAppSettings(string key, string value) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException) { Console.WriteLine("Error writing app settings"); } } 

有关更多信息,请参阅MSDN

首选<appSettings><customUserSetting>部分。 使用(Web)ConfigurationManager读取和写入要容易得多。 ConfigurationSection,ConfigurationElement和ConfigurationElementCollection要求您派生自定义类并实现自定义的ConfigurationProperty属性。 对于日常凡人IMO来说太过分了。

这是一个读写web.config的例子:

 using System.Web.Configuration; using System.Configuration; Configuration config = WebConfigurationManager.OpenWebConfiguration("/"); string oldValue = config.AppSettings.Settings["SomeKey"].Value; config.AppSettings.Settings["SomeKey"].Value = "NewValue"; config.Save(ConfigurationSaveMode.Modified); 

之前:

 <appSettings> <add key="SomeKey" value="oldValue" /> </appSettings> 

后:

 <appSettings> <add key="SomeKey" value="newValue" /> </appSettings> 

作为基本的问题是关于胜利forms这里是解决scheme:(如果它是一个新的密钥,我刚刚更改了代码由user1032413来反映windowsForms设置):

 Configuration config = configurationManager.OpenExeConfiguration(Application.ExecutablePath); config.AppSettings.Settings.Add("Key","Value"); config.Save(ConfigurationSaveMode.Modified); 

如果密钥已经存在:

 Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); config.AppSettings.Settings["Key"].Value="Value"; config.Save(ConfigurationSaveMode.Modified); 

在保存电话后尝试添加。

 ConfigurationManager.RefreshSection( "appSettings" ); 

请记住,ConfigurationManager只使用一个app.config – 启动项目中的一个。

如果你把一些app.config放到解决schemeA中,并从另一个解决schemeB中引用它,那么如果你运行B,那么A中的app.config将被忽略。

所以例如unit testing项目应该有自己的app.config。

我认为问题是,在debuggingVisual Studio中不要使用正常的exeName。

它使用独立的“NameApplication”.host.exe

所以configuration文件的名称是“NameApplication”.host.exe.config而不是“NameApplication”.exe.config

并在应用程序closures后 – 它返回到后面的app.config

所以如果你检查错误的文件,或者检查错误的时间,你会发现没有任何改变。

 public void ApplySettings(string key, string value) { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } 

您可以手动更改它:

 private void UpdateConfigFile(string appConfigPath, string key, string value) { var appConfigContent = File.ReadAllText(appConfigPath); var searchedString = $"<add key=\"{key}\" value=\""; var index = appConfigContent.IndexOf(searchedString) + searchedString.Length; var currentValue = appConfigContent.Substring(index, appConfigContent.IndexOf("\"", index) - index); var newContent = appConfigContent.Replace($"{searchedString}{currentValue}\"", $"{searchedString}{newValue}\""); File.WriteAllText(appConfigPath, newContent); }