15

私は試した

<appSettings >
    <add key="List" value="1"/>
    <add key="List" value="2"/>
    <add key="List" value="3"/>
  </appSettings >

System.Configuration.ConfigurationManager.AppSettings.GetValues("List");

しかし、私は最後のメンバーしか取得しません。どうすればこれを簡単に解決できますか?

4

5 に答える 5

27

私は同様の問題を扱い、このコードでそれを行いました。これがあなたの問題に役立つことを願っています。

この場合、リスト(私のURLSectionと同様)には、web.configに完全な構成セクションがあり、このセクションからすべての値を取得できます。

<configSections>
    <section name="URLSection" type="A.WebConfigSection,A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>

<appSettings></appSettings>

<URLSection>
    <urlCollection>
        <add url="1" value="a"/>
        <add url="2" value="b"/>
    </urlCollection>
</URLSection>

このために、ConfigElement、ConfigElementCollection、WebConfigSectionの3つのクラスを作成しました。

ConfigElement

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
  public class ConfigElement:System.Configuration.ConfigurationElement
{
    [ConfigurationProperty("url",IsRequired=true) ]
    public string url
    {
        get
        {
            return this["url"] as string;
        }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string value
    {
        get
        {
            return this["value"] as string;
        }
    }



  }
}

ConfigElementCollection

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
  public class ConfigElementCollection:ConfigurationElementCollection
 {
    public ConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as ConfigElement;
        }

    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ConfigElement)(element)).url;
    }
 }
}

WebConfigSection

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace A
{
 public class WebConfigSection:ConfigurationSection
 {

    public WebConfigSection()
    {

    }

    [ConfigurationProperty("urlCollection")]
    public ConfigElementCollection allValues
    {
        get
        {
            return this["urlCollection"] as ConfigElementCollection;
        }
    }

    public static WebConfigSection GetConfigSection()
    {
        return ConfigurationSettings.GetConfig("URLSection") as WebConfigSection;
    }
 }
}
于 2009-11-18T11:56:46.297 に答える
4
    foreach (string str in ConfigurationManager.AppSettings.AllKeys)
    {
        if (str.ToUpper().IndexOf("SOMESPECIAL") > -1) //the somespecial ones you want to add in
            lstList.Add(ConfigurationManager.AppSettings[str]);
    }
于 2012-01-13T17:39:19.607 に答える
3

NinjaSettingsは、これをそのまま実行します。

パッケージマネージャーコンソールで

Install-Package NinjaSettings

あなたはあなたのリストを次のように宣言するでしょう

  <appSettings>
    <add key="List" value="50,20,10,100"/>
  </appSettings>

次に、任意のICollectionまたは配列へのリストのマッピングを使用してインターフェイスを作成します

public interface IAppSettings
{
    List<int> List { get; }
}

次に、設定ユーザーにNinjaSettingsラッパーにアクセスします。通常、これはIOCを使用して配線しますが、基本的な使用法は次のとおりです。

   var settings = new NinjaSettings<IAppSettings>().Settings;

   int total = 0;
   for (var i in settings.List) 
   {
      total+=i;        
   }
于 2013-12-31T06:24:50.660 に答える
1

Haackedは、構成設定への簡潔なアプローチを提供します。彼のアプローチでは、ConfigurationSectionから派生した1つのクラスを使用します。したがって、彼のブログの例では、app.configまたはweb.configxml表現は次のようになります。

<configuration>
  <configSections>
    <section name="BlogSettings" type="Fully.Qualified.TypeName.BlogSettings,   
      AssemblyName" />
  </configSections>
  <BlogSettings frontPagePostCount="10" title="You’ve Been Haacked" />
</configuration>

これは一読の価値があります:

http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

于 2011-09-30T13:35:53.280 に答える
1

この情報を別のXMLファイルに入れ、AppSettingsにそのファイルへの参照を含める方がよいでしょう。これにより、情報を取得して消費する方法について、より柔軟に対応できるようになります。

唯一のことは、System.Configuration.ConfigurationManager.AppSettingsクラスと同様の方法でXMLを読み取るための別個の(静的?)クラスを作成することです。

一方、Web.Configファイルに含まれている必要がある場合、これを実現する唯一の方法は、[パイプ/コンマ/セミコロン]で区切られた配列を1つの「リスト」設定に含めることです。 。

于 2009-11-18T11:42:31.810 に答える