appsetting节点c#中的子appsettings
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14812008/
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
sub appsettings in the appsetting node c#
提问by Tony Germanos
I am using the app.config file that is created with a console application and I can read the val1 of the key1 using the ConfigurationSettings.AppSettings["key1"].ToString()
我正在使用通过控制台应用程序创建的 app.config 文件,我可以使用以下命令读取 key1 的 val1 ConfigurationSettings.AppSettings["key1"].ToString()
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<appSettings>
<add key="key1" value="val1" />
<add key="key2" value="val2" />
</appSettings>
</configuration>
but I have too many keys and values that I want to make them categorized.
但我有太多的键和值,我想让它们分类。
I found something that is difficult to use in my application since I want to access the keys in a similar way to the above one
我发现一些难以在我的应用程序中使用的东西,因为我想以与上述类似的方式访问密钥
Showing all nodes and can't read a node without getting all the nodes
for example what I want to do:
例如我想做什么:
<appSettings>
<Section1>
<add key="key1" value="val1" />
</Section1>
<Section2>
<add key="key1" value="val1" />
<Section2>
</appSettings>
and if there is a way to access it using
ConfigurationSettings.AppSettings["Section1"].["key1"].ToString()
如果有办法使用它访问它
ConfigurationSettings.AppSettings["Section1"].["key1"].ToString()
采纳答案by Konrad Gadzina
You can add custom sections in app.config without writing additional code. All you have to do is "declaring" new section in configSections
node like that
您可以在 app.config 中添加自定义部分,而无需编写额外的代码。您所要做的就是configSections
像这样在节点中“声明”新部分
<configSections>
<section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
and then you can define this section filling it with keys and values:
然后你可以定义这个部分,用键和值填充它:
<genericAppSettings>
<add key="testkey" value="generic" />
<add key="another" value="testvalue" />
</genericAppSettings>
To get value of a key from this section you have to add System.Configuration
dll as reference to your project, add using
and use GetSection
method. Example:
要从此部分获取键的值,您必须添加System.Configuration
dll 作为对项目的引用,添加using
和使用GetSection
方法。例子:
using System.Collections.Specialized;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("genericAppSettings");
string a = test["another"];
}
}
}
Nice thing is that you can easily make groups of sections if you need this:
不错的是,如果您需要,您可以轻松地将部分分组:
<configSections>
<sectionGroup name="customAppSettingsGroup">
<section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
// another sections
</sectionGroup>
</configSections>
<customAppSettingsGroup>
<genericAppSettings>
<add key="testkey" value="generic" />
<add key="another" value="testvalue" />
</genericAppSettings>
// another sections
</customAppSettingsGroup>
If you use groups, to access sections you have to access them using {group name}/{section name}
format:
如果您使用组,要访问部分,您必须使用{group name}/{section name}
格式访问它们:
NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("customAppSettingsGroup/genericAppSettings");
回答by Ikaso
AFAIK you can implement a custom section outside of the appsettings. For example, frameworks like Autofac and SpecFlow use these kind of sessions to support their own configuration schema. You can take a look on thisMSDN article to understand how to do that. Hope that helps.
AFAIK 您可以在 appsettings 之外实现自定义部分。例如,像 Autofac 和 SpecFlow 这样的框架使用这些类型的会话来支持他们自己的配置模式。您可以查看此MSDN 文章以了解如何执行此操作。希望有帮助。