app.config中的节类型

Sil*_*ver 4 c# app-config appsettings configsection

我正在研究如何在app.config的configsection中创建节组和节。所以,让我们取样

    <configSections>
    <sectionGroup name="trackGroup">
    <section name="trackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </configSections>
    <trackGroup>
    <trackInfo>
    <add key = "ASHUM" value="ASH-UM"/>
    </trackInfo>
</trackGroup>
Run Code Online (Sandbox Code Playgroud)

因此,当我浏览各种文章时,我发现每个人的节使用不同的类型。因此,我在这里发现了不同的类型:http : //msdn.microsoft.com/zh-cn/library/aa309408%28v=vs.71 %29.aspx

但是这里没有提到我使用的类型。我从一个随机示例中获得了这种类型,据我了解,它实际上是在为appsetting部分定义设置。有人可以帮助我,我的样本中的类型是什么意思,我的意思是版本,公共令牌,文化如何定义这些内容?我也想知道哪种类型更好用?就像我必须在运行时访问这些设置,甚至在运行时进行一些修改。

另外我想这些不同的类型有不同的访问方式?像上面示例中的情况一样,我正在按以下方式访问键和值:

  static void getFull(string sectionName)
        {
            System.Collections.Specialized.NameValueCollection section = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection(sectionName);
            if (section != null)
            {
                foreach (string key in section.AllKeys)
                {
                    Console.WriteLine(key + ": " + section[key]);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是,如果我们使用MSDN链接中提供的类型,那么我将如何访问键和值?

小智 5

这是我创建配置部分时要做的事情。它还允许外部化部分。

App.config

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="TrackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </configSections>

    <TrackInfo configSource="TrackInfo.config"/>
</configuration>
Run Code Online (Sandbox Code Playgroud)

TrackInfo.config

<?xml version="1.0" encoding="utf-8" ?>
<TrackInfo>
    <add key="SOME_KEY" value="SOME_VALUE" />
</TrackInfo>
Run Code Online (Sandbox Code Playgroud)

C#

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection( "TrackInfo" );
if( null == section ) throw new Exception( "Missing TrackInfo.config" );

string val = section["SOME_KEY"];
Run Code Online (Sandbox Code Playgroud)

  • 将2.0.0.0用于框架2.0到3.5.1。对于4.x使用4.0.0.0。令牌是表示用来签名该程序集的加密信息的令牌。必须正确 (2认同)