任何人都可以请帮助我如何使用c#在app.config文件中设置/存储值,是否有可能?在app.config文件中写入值
36
A
回答
6
如果您使用App.Config来存储<app Key="" Value="" />
或CustomSections部分中的值,请使用ConfigurationManager类,否则使用XMLDocument类。
你可以使用贴在CodeProject
2
是的,你可以 - 看到ConfigurationManager
ConfigurationManager中类 包括成员,使您能够 执行以下任务:
- 读写配置文件作为一个整体。
学会使用docs,他们应该是你的第一个端口的呼吁,像这样的问题。
32
代码试试下面的代码:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add("YourKey", "YourValue");
config.Save(ConfigurationSaveMode.Minimal);
它为我:-)
16
在框架4.5的AppSettings.Settings [ “钥匙” ] ConfigurationManager的一部分是只读的,所以我必须先删除密钥,然后使用以下内容再次添加它:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");
config.Save(ConfigurationSaveMode.Modified);
不要担心,如果您尝试删除不存在的密钥,则不会发生异常。
这post给出了一些很好的意见
10
private static string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
private static void SetSetting(string key, string value)
{
Configuration configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
}
0
string filePath = System.IO.Path.GetFullPath("settings.app.config");
var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
try
{
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
// Add an Application Setting if not exist
config.AppSettings.Settings.Add("key1", "value1");
config.AppSettings.Settings.Add("key2", "value2");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
catch (ConfigurationErrorsException ex)
{
if (ex.BareMessage == "Root element is missing.")
{
File.Delete(filePath);
return;
}
MessageBox.Show(ex.Message);
}
2
对于.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");
}
2
尝试以下操作:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.Save();
ConfigurationManager.RefreshSection("appSettings");
4
正如其他人所提到的,你可以用ConfigurationManager.AppSettings.Settings
做到这一点。但是: 如果密钥不存在,则使用Settings[key] = value
将不起作用。
使用Settings.Add(key, value)
,如果键已经存在,它会加入新的价值,它的价值(S)由逗号分隔,像 <add key="myKey" value="value1, value2, value3" />
为了避免这些意外的结果,你必须处理两个场景的
- 如果使用给定密钥的条目存在?那么更新其值
- 如果使用给定密钥输入不存在?然后创建新条目(键,值)
代码
public static void Set(string key, string value)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var entry = config.AppSettings.Settings[key];
if (entry == null)
config.AppSettings.Settings.Add(key, value);
else
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
}
有关检查entry == null
更多信息,请this post。
希望这会帮助别人。
相关问题
- 1. 在app.config文件中写入bindingRedirect元素
- 2. C#写入App.config文件
- 3. 在.Net 3.5中写入app.config?
- 4. 读取\写入到外部app.config文件
- 5. 如何在控制台应用程序中将值永久写入app.config文件?
- 6. C#app.config读取和写入
- 7. 动态更改app.config文件中的值
- 8. 写在app.config中的.Net
- 9. 在Monotouch中写入文件
- 10. 在文件中写入列
- 11. 在文件中写入
- 12. 在Python中写入文件
- 13. 在Clojure中写入文件
- 14. 将p值写入R中的文件
- 15. .NET app.config文件
- 16. C#App.Config文件
- 17. PowerShell:在输入类型=文件窗体中写入值。
- 18. 在java中使用多线程在文件中写入值
- 19. 将cout值写入文本文件
- 20. 如何在内存中写入文件或在Lambda aws中写入文件?
- 21. 删除硬编码值到app.config文件
- 22. 在java中写入和读入文件
- 23. XBAP中的App.config文件
- 24. 使用python在文本文件中写入多个值
- 25. WPF的app.config写
- 26. 在Python中将文本写入文件
- 27. 在Matlab中写入文本文件
- 28. 在swift中写入文本文件3
- 29. 在app.config文件中传递十六进制值
- 30. 如何在app.config文件中设置连接字符串值?
你这谷歌这一切? – 2011-01-21 12:08:43
你能更具体一点吗?你想存储什么样的价值?什么时候?通常不推荐在应用程序配置中存储应用程序范围的值(如字符串或整数)。 – Mantisen 2011-01-21 12:09:04
提出一个看起来很愚蠢的问题是没问题的,每个人都从一开始就开始。然而,甚至没有采取一个简单的谷歌搜索只是跛脚。 – 2011-01-21 12:25:57