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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:22:17  来源:igfitidea点击:

Add values to app.config and retrieve them

c#

提问by devnull

I need to insert key value pairs in app.Config as follows:

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

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

When I searched in google I got the following code snippet

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

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

The above code is not working as ConfigurationManager is not found in System.Configuration namespace I'm using .NET 2.0. How to add key-value pairs to app.Config programatically and retrieve them?

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

采纳答案by Arjan Einbu

Are you missing the reference to System.Configuration.dll? ConfigurationManagerclass lies there.

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

EDIT: The System.Configurationnamespace has classes in mscorlib.dll, system.dll and in system.configuration.dll. Your project always include the mscorlib.dll and system.dll references, but system.configuration.dll must be added to most project types, as it's not there by default...

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

回答by OneSHOT

Try adding a Reference to System.Configuration, you get some of the configuration namespace by referencing the System namespace, adding the reference to System.Configuration should allow you to access ConfigurationManager.

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

回答by Radicz

This works:

这有效:

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

回答by Suraj

I hope this works:

我希望这有效:

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

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

回答by Thomas

sorry for late answer but may be my code may help u.

抱歉迟到了,但可能是我的代码可以帮助你。

I placed 3 buttons on the winform surface. button1 & 2 will set different value and button3 will retrieve current value. so when run my code first add the reference System.configuration

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

and click on first button and then click on 3rd button to see what value has been set. next time again click on second & 3rd button to see again what value has been set after change.

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

so here is the code.

所以这是代码。

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 Junaid Khan

To Get The Data From the App.config

从 App.config 获取数据

Keeping in mind you have to:

请记住,您必须:

  1. Added to the References -> System.Configuration
  2. and also added this using statement -> using System.Configuration;
  1. 添加到参考文献 -> System.Configuration
  2. 并且还添加了这个 using 语句 -> using System.Configuration;

Just Simply do this

只需简单地做到这一点

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

Alternatively, you can achieve this in one line, if you don't want to add using System.Configuration;explicitly.

或者,如果您不想using System.Configuration;明确添加,您可以在一行中实现这一点。

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