C# AppSettings:有没有一种简单的方法将集合放入中?

发布于 2024-08-11 08:23:40 字数 327 浏览 4 评论 0原文

我尝试了

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

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

但我只得到了最后一个成员。 我怎样才能轻松解决这个问题?

i tried

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

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

But i only get the last member .
How could i solve this easily?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独闯女儿国 2024-08-18 08:23:40

我处理过类似的问题,并用这段代码做到了。希望这对您的问题有所帮助。

在这种情况下,列表(类似于我的 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。

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;
    }
 }
}

I have dealt a similar issue and I did it with this code. Hope this helps in your problem.

In this case List (similar to my URLSection) will have a full configuration Section in web.config which you can get all values from this section then.

<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>

I made three classes for this: ConfigElement, ConfigElementCollection, WebConfigSection.

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;
    }
 }
}
全部不再 2024-08-18 08:23:40
    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]);
    }
    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]);
    }
弃爱 2024-08-18 08:23:40

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;        
   }

NinjaSettings does this out of the box.

In the package manager console

Install-Package NinjaSettings

You would declare your list as

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

then create an Interface with a mapping for list to any ICollection or Array

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

then access your settings user the NinjaSettings wrapper. Generally you would wire this up using IOC, but the basic usage is

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

   int total = 0;
   for (var i in settings.List) 
   {
      total+=i;        
   }
傾城如夢未必闌珊 2024-08-18 08:23:40

您最好将此信息放在单独的 XML 文件中,并在 AppSettings 中引用该文件。这将使您在检索和使用信息的方式方面拥有更大的灵活性。

唯一的问题是您需要创建一个单独的(静态?)类,用于以与 System.Configuration.ConfigurationManager.AppSettings 类类似的方式读取 XML。

另一方面,如果它必须位于您的 Web.Config 文件中,我建议实现此目的的唯一方法就是在一个“列表”设置中使用 [管道/逗号/分号] 分隔数组。

You'd likely be better off putting this information in a separate XML file and having a reference to that file in AppSettings. That would give you a lot more flexibility around how you retrieved the information and consumed it.

The only thing would be that you'd want to create a separate (static?) class for reading the XML in a similar fashion to the System.Configuration.ConfigurationManager.AppSettings class.

If, on the other hand, it HAD to be in your Web.Config file, I would suggest the only way to achieve this simply would be to have a [pipe/comma/semi-colon] delimited array in one "List" setting.

九命猫 2024-08-18 08:23:40

Haacked 提供了一种简洁的配置设置方法。他的方法使用一个派生自 ConfigurationSection 的类。因此,对于他的博客示例,您的 app.config 或 web.config xml 表示将如下所示:

<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

Haacked provides a concise approach to configuration settings. His approach uses one class deriving from ConfigurationSection. So for his blog example your app.config or web.config xml representation will look like this:

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

This is worth a read:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文