appsetting节点c#中的子appsettings [英] sub appsettings in the appsetting node c#
问题描述
我正在使用通过控制台应用程序创建的 app.config 文件,我可以使用 ConfigurationSettings.AppSettings["key1"].ToString()
读取 key1 的 val1><预><代码><配置><启动><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></启动><应用设置><add key="key1" value="val1"/><add key="key2" value="val2"/></appSettings></配置>
但是我有太多的键和值,我想让它们分类.
我发现一些难以在我的应用程序中使用的东西,因为我想以与上述类似的方式访问密钥
例如我想做什么:
<第一节><add key="key1" value="val1"/></Section1><第2节><add key="key1" value="val1"/><第2节></appSettings>
如果有办法使用它访问它ConfigurationSettings.AppSettings["Section1"].["key1"].ToString()
您可以在 app.config 中添加自定义部分,而无需编写额外的代码.您所要做的就是在 configSections
节点中声明"新部分
<section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></configSections>
然后你可以定义这个部分,用键和值填充它:
<add key="testkey" value="generic"/><add key="another" value="testvalue"/></genericAppSettings>
要从此部分获取键的值,您必须添加 System.Configuration
dll 作为对您项目的引用,添加 using
并使用 GetSection代码>方法.示例:
using System.Collections.Specialized;使用 System.Configuration;命名空间 ConsoleApplication1{课程计划{static void Main(string[] args){NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("genericAppSettings");string a = test["another"];}}}
如果需要,您可以轻松地将部分分组:
<section name="genericAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>//其他部分</sectionGroup></configSections><通用应用设置><add key="testkey" value="generic"/><add key="another" value="testvalue"/></genericAppSettings>//其他部分</customAppSettingsGroup>
如果您使用组,要访问部分,您必须使用 {group name}/{section name}
格式访问它们:
NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("customAppSettingsGroup/genericAppSettings");
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()
<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()
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
<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:
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:
NameValueCollection test = (NameValueCollection)ConfigurationManager.GetSection("customAppSettingsGroup/genericAppSettings");
这篇关于appsetting节点c#中的子appsettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!