2

次のようなapp.configを作成したいと思います

<configuration>

<SQLconneciton>
  <add key=name/>
  <add key= otherStuff/>
</SQLconnection>
<PacConnection>
  <add key=name/>
  <add key= otherStuff/>
</PacConnection>

</configuration>

私は人々が1つのカスタムセクションを作成してものを追加する多くの例を読みました。ユーザーが複数のセクションを追加、読み取り、削除できるようにする必要があります。派手な要素は本当に必要ありません。単純な加算値とキー値だけです。セクショングループは使用する価値がありますか、それとも私が見逃している簡単なものがありますか?

4

1 に答える 1

1

もちろん、カスタム構成セクションを好きなだけ作成することを妨げるものは何もありません。

次のようなものを試してください。

<?xml version="1.0"?>
<configuration>
  <!-- define the config sections (and possibly section groups) you want in your config file -->
  <configSections>
    <section name="SqlConnection" type="System.Configuration.NameValueSectionHandler"/>
    <section name="PacConnection" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <!-- "implement" those config sections as defined above -->
  <SqlConnection>
    <add key="abc" value="123" />
  </SqlConnection>
  <PacConnection>
    <add key="abc" value="234" />
  </PacConnection>
</configuration>

は、エントリ(など)System.Configuration.NameValueSectionHandlerを含む構成セクションに使用するデフォルトのタイプです。<add key="...." value="....." /><appSettings>

値を取得するには、次のようなものを使用します。

NameValueCollection sqlConnConfig = ConfigurationManager.GetSection("SqlConnection") as NameValueCollection;
string valueForAbc = sqlConnConfig["abc"];

また、.NETで定義されている既存のセクションハンドラータイプと、独自のカスタム構成セクションを自分で定義している場合は、それらを完全に組み合わせることができます。必要なものを使用してください。

于 2013-01-03T21:29:08.527 に答える