C# 如何从 WinForms 中的 app.config 读取 AppSettings

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12731683/
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-10 00:15:31  来源:igfitidea点击:

How to read AppSettings from app.config in WinForms

c#app-config

提问by John Ryann

I usually use a text file as a config. But this time I would like to utilize app.config to associate a file name (key) with a name (value) and make the names available in combo box

我通常使用文本文件作为配置。但是这次我想利用 app.config 将文件名(键)与名称(值)相关联,并使名称在组合框中可用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
   <add key="Scenario1.doc" value="Hybrid1"/>
   <add key="Scenario2.doc" value="Hybrid2"/>
   <add key="Scenario3.doc" value="Hybrid3"/>
</appSettings>
</configuration>

will this work? how to retrieve the data ?

这会起作用吗?如何检索数据?

回答by Gromer

Straight from the docs:

直接来自文档

using using System.Configuration;

// Get the AppSettings section.        
// This function uses the AppSettings property
// to read the appSettings configuration 
// section.
public static void ReadAppSettings()
{
  try
  {
    // Get the AppSettings section.
    NameValueCollection appSettings = ConfigurationManager.AppSettings;

    // Get the AppSettings section elements.
    Console.WriteLine();
    Console.WriteLine("Using AppSettings property.");
    Console.WriteLine("Application settings:");

    if (appSettings.Count == 0)
    {
      Console.WriteLine("[ReadAppSettings: {0}]",
      "AppSettings is empty Use GetSection command first.");
    }
    for (int i = 0; i < appSettings.Count; i++)
    {
      Console.WriteLine("#{0} Key: {1} Value: {2}",i, appSettings.GetKey(i), appSettings[i]);
    }
  }
  catch (ConfigurationErrorsException e)
  {
    Console.WriteLine("[ReadAppSettings: {0}]", e.ToString());
  }
}

So, if you want to access the setting Scenario1.doc, you would do this:

所以,如果你想访问设置Scenario1.doc,你可以这样做:

var value = ConfigurationManager.AppSettings["Scenario1.doc"];

var value = ConfigurationManager.AppSettings["Scenario1.doc"];

Edit:

编辑:

As Gabriel GM said in the comments, you will have to add a reference to System.Configuration.

正如 Gabriel GM 在评论中所说,您必须添加对System.Configuration.

回答by Pavan Josyula

app settings in app.config are to store application/environment specific settings not to store data which binds to UI.

app.config 中的应用程序设置用于存储应用程序/环境特定的设置,而不是存储绑定到 UI 的数据。

If you cant avoid storing in config because of weird business requests I would rather stick to one single setting

如果由于奇怪的业务请求而无法避免存储在配置中,我宁愿坚持一个设置

<add key="FileDropDown" value="File1-Value|File2-Value" />

and write C# code to get this setting ConfigurationManager.AppSettings["FileDropDown"]and do some string Splits ('|') and ('-') to create kvp collection and bind it to UI.

并编写 C# 代码以获取此设置ConfigurationManager.AppSettings["FileDropDown"]并执行一些字符串拆分 ('|') 和 ('-') 以创建 kvp 集合并将其绑定到 UI。