共用方式為


<section> 項目

包含組態區段宣告。

<configuration>
<configSections>
<section>

<configuration>
<configSections>
<sectionGroup>
<section>

<section
   name="section name"
   type="configuration section handler class, assembly"
   allowDefinition="Everywhere|MachineOnly|MachineToApplication"    allowLocation="true|false" />

必要屬性

屬性 說明
name 指定組態區段的名稱。
type 指定從組態檔讀取區段的組態區段處理常式類別的名稱。

選擇性屬性

下列屬性僅適用於 ASP.NET 應用程式。當其他類型的應用程式正在執行時,組態系統會忽略這些屬性。

屬性 說明
allowDefinition 指定要在哪一個組態檔中使用區段。請使用下列其中一個值:
Everywhere
允許在任何組態檔中使用區段。這是預設值。
MachineOnly
只允許在電腦組態檔 (Machine.config) 中使用區段。
MachineToApplication
允許在電腦組態檔或應用程式組態檔中使用區段。
allowLocation 決定在 <location> 項目內是否可以使用區段。使用下列其中一個值:
true
允許在 <location> 項目內使用區段。這是預設值。
false
不允許在 <location> 項目內使用區段。

備註

宣告組態區段其實是為組態檔定義新的項目。新的項目包含組態區段處理常式 (也就是,實作 IConfigurationSectionHandler 介面的類別) 所讀取的設定。您定義的屬性和子項目取決於您用來讀取設定的區段處理常式。

在 Machine.config 檔案中宣告組態區段處理常式,可以讓您使用該電腦上任何應用程式組態檔中的組態區段,除非 allowDefinition 屬性指定了其他選項。

範例

下列範例顯示如何定義組態區段,以及定義該區段的設定。

<configuration>
   <configSections>
      <section name="sampleSection"
               type="System.Configuration.SingleTagSectionHandler" 
               allowLocation="false"/>
   </configSections>
   <sampleSection setting1="Value1" setting2="value two" 
                  setting3="third value" />
</configuration>

組態檔

這個項目可以在不是位於應用程式目錄層級的應用程式組態檔、電腦組態檔 (Machine.config) 和 Web.config 檔案中使用。

請參閱

NameValueSectionHandler 和 DictionarySectionHandler 的自訂項目 | SingleTagSectionHandler 的自訂項目 | 組態區段設定 | <sectionGroup> 項目 | 組態區段結構描述