2011-01-21 65 views
36

任何人都可以请帮助我如何使用c#在app.config文件中设置/存储值,是否有可能?在app.config文件中写入值

+11

你这谷歌这一切? – 2011-01-21 12:08:43

+1

你能更具体一点吗?你想存储什么样的价值?什么时候?通常不推荐在应用程序配置中存储应用程序范围的值(如字符串或整数)。 – Mantisen 2011-01-21 12:09:04

+3

提出一个看起来很愚蠢的问题是没问题的,每个人都从一开始就开始。然而,甚至没有采取一个简单的谷歌搜索只是跛脚。 – 2011-01-21 12:25:57

回答

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
希望这会帮助别人。