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


75

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

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

当我在Google中搜索时,我得到了以下代码片段

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);

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


6
对于任何阅读此内容的人来说,这只是一条注释,config.Save(ConfigurationSaveMode.Modified)仅在app.config文件中预定义了密钥时才保存值。要真正创建一个新的,只需使用不带参数的config.Save()即可。
ryanulit

Answers:


53

您是否缺少对System.Configuration.dll的引用?ConfigurationManager班级在那里。

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


2
有时候我会得到的!我一直认为我只需要使用:(
crashmstr

12

这有效:

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

3
如果您希望能够访问此值,则只需稍后在代码中添加它而无需重新启动应用程序,则需要ConfigurationManager.RefreshSection("appSettings");在此之后调用。
传播

您可以使用Assembly.GetExecutingAssembly().Location而不是Application.ExecutablePath控制台应用程序中运行它。您也可以查看Suraj的答案
AlexMelw

9

尝试添加Reference System.Configuration,通过引用System命名空间,您将获得一些配置名称空间,将对System.Configuration的引用添加到您应该可以访问ConfigurationManager


5

我希望这行得通:

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

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

3

从App.config获取数据

请记住,您必须:

  1. 添加到参考-> System.Configuration
  2. 并还添加了using语句-> using System.Configuration;

只要做到这一点

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

另外,如果您不想using System.Configuration;显式添加,也可以一行完成。

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

0

抱歉,您的回答很晚,但可能是我的代码可能对您有所帮助。

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

然后单击第一个按钮,然后单击第三个按钮以查看已设置的值。下次再次单击第二和第三按钮以再次查看更改后已设置的值。

所以这是代码。

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);
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.