在appsetting节点c#子的AppSettings [英] sub appsettings in the appsetting node c#

查看:1419
本文介绍了在appsetting节点c#子的AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是与一个控制台应用程序创建的app.config文件,我可以使用 ConfigurationSettings.AppSettings [键1]读取键1的VAL1。的ToString()

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


<&启动GT; < supportedRuntime版本=V4.0SKU =.net框架,版本= V4.0。/>
< /启动>
`

<startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> `

<add key="key1" value="val1" />
<add key="key2" value="val2" />



< /的appSettings>
< /结构>

但我有太多的键和值,我想给他们归类

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

显示所有节点,并没有得到所有的节点都无法读取节点

例如我想要做的:

<appSettings>
<Section1>
     <add key="key1" value="val1" />
</Section1>
<Section2>
     <add key="key1" value="val1" />
<Section2>
</appSettings>

和是否有用来访问它
ConfigurationSettings方式.AppSettings [SECTION1] [键1]。的ToString()

推荐答案

您可在app.config中添加自定义的部分,而无需编写额外的代码。所有你需要做的就是声明,在新的一节 configSections 这样的节点

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>

要获得这部分你必须添加系统的一个关键的价值。配置 DLL作为参考您的项目中,添加使用,并使用 GetSection 方法。例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆