簡體   English   中英

app.config中的節類型

[英]section type in app.config

我正在研究如何在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>

因此,當我瀏覽各種文章時,我發現每個人的節使用不同的類型。所以,我在這里發現了不同的類型: 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]);
                }
            }
        }

但是,如果我們使用MSDN鏈接中提供的類型,那么我將如何訪問鍵和值?

這是我創建配置部分時要做的事情。 它還允許外部化部分。

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>

TrackInfo.config

<?xml version="1.0" encoding="utf-8" ?>
<TrackInfo>
    <add key="SOME_KEY" value="SOME_VALUE" />
</TrackInfo>

C#

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

string val = section["SOME_KEY"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM