将值添加到app.config并检索它们

时间:2009-05-29 11:52:54

标签: c#

我需要在app.Config中插入键值对,如下所示:

<configuration>
 <appSettings>
    <add key="Setting1" value="Value1" />
    <add key="Setting2" value="Value2" />
 </appSettings>
</configuration>

当我在谷歌搜索时,我得到以下代码片段

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an Application Setting.

config.AppSettings.Settings.Add("ModificationDate",
               DateTime.Now.ToLongTimeString() + " ");

// Save the changes in App.config file.

config.Save(ConfigurationSaveMode.Modified);

上面的代码无效,因为System.Configuration命名空间中找不到ConfigurationManager我正在使用.NET 2.0。 如何以编程方式将键值对添加到app.Config并检索它们?

6 个答案:

答案 0 :(得分:50)

您是否缺少对System.Configuration.dll的引用?那里有ConfigurationManager课。

编辑:System.Configuration命名空间在mscorlib.dll,system.dll和system.configuration.dll中有类。您的项目始终包含mscorlib.dll和system.dll引用,但必须将system.configuration.dll添加到大多数项目类型中,因为它默认不存在...

答案 1 :(得分:11)

这很有效。

  public static void AddValue(string key, string value)
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
        config.AppSettings.Settings.Add(key, value);
        config.Save(ConfigurationSaveMode.Minimal);
    }

答案 2 :(得分:8)

尝试添加对System.Configuration的引用,通过引用System命名空间获得一些配置命名空间,添加对System.Configuration的引用,允许您访问ConfigurationManager

答案 3 :(得分:5)

我希望这有效:

System.Configuration.Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["Yourkey"].Value = "YourValue";
config.Save(ConfigurationSaveMode.Modified);

答案 4 :(得分:3)

要从App.config中获取数据

记住,您必须:

  1. 已添加到参考中-> System.Configuration
  2. ,还使用语句-> using System.Configuration;
  3. 添加了此内容

只需执行此操作

string value1 = ConfigurationManager.AppSettings["Value1"];

或者,如果您不想显式添加using System.Configuration;,则可以一行完成。

string value1 = System.Configuration.ConfigurationManager.AppSettings["Value1"]

答案 5 :(得分:0)

对不起,迟到的答案可能是我的代码可能会帮助你。

我在winform表面放了3个按钮。 button1&amp; 2将设置不同的值,button3将检索当前值。因此,在运行我的代码时,首先添加引用System.configuration

然后单击第一个按钮,然后单击第三个按钮以查看已设置的值。 下次再点击第二个&amp;第3个按钮,可以再次查看更改后设置的值。

所以这是代码。

using System.Configuration;

 private void button1_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            config.AppSettings.Settings.Remove("DBServerName");
            config.AppSettings.Settings.Add("DBServerName", "FirstAddedValue1");
            config.Save(ConfigurationSaveMode.Modified);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            config.AppSettings.Settings.Remove("DBServerName");
            config.AppSettings.Settings.Add("DBServerName", "SecondAddedValue1");
            config.Save(ConfigurationSaveMode.Modified);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            MessageBox.Show(config.AppSettings.Settings["DBServerName"].Value);
        }
相关问题