在appSettings中为一个键使用多个值

时间:2010-11-18 13:57:01

标签: c# configuration-files

我正在学习如何使用配置文件,我遇到了一些问题,我希望有人可以给我一些建议。无论我的文件是否为XML都没关系,但我读过的大部分例子都是使用它们而且我只是为了让生活更轻松的事情。

我遇到的问题是appSettings文件似乎设置为只接受一个键的一个值,我希望有类似的东西:

<key="Machine List" value="Server105" />
<key="Machine List" value="Server230" />

我找到了hack here,但它是在6年前编写的,我不知道是否有更好的方法。

同样,如果这是XML,平面文件等也无关紧要....我只是想学习如何使用配置文件而不是硬编码值直接进入应用程序。

感谢您的帮助。

4 个答案:

答案 0 :(得分:10)

如果你真的需要在密钥下存储多台机器,那么更合适:

<key="Machine List" value="Server105,Server230" />

分隔符是您选择的字符。

答案 1 :(得分:10)

条目属性的替代方法是将子节点添加到设置节点:

 <setting key="Machine List">
     <value>Server105</value>
     <value>Server230</value>
   </setting>

这样您就不需要字符串操作来提取不同的值。

答案 2 :(得分:1)

您可以使用配置部分来定义自己的配置。只需添加

<configSections>
  <sectionGroup name="MyConfiguration">
    <section name="MyQuery" type="namespace.QueryConfigurationSection" allowLocation="true" allowDefinition="Everywhere"/>
  </sectionGroup>
</configSections>

<configuration>之后,您可以在appsetting之后添加自定义部分

</appSettings>
<!-- custom query configuration -->
<MyConfiguration>
 <MyQuery>     
  <Query1> </Query1>
  <Query2> </Query2>

要阅读,您需要创建几个类

/// <summary>
/// Creates a custom configuration section inside web.config
/// </summary>
public class QueryConfigurationSection : ConfigurationSection
{
    //query 2
    [ConfigurationProperty("Query1")]
    public QueryElement1 Query1
    {
        get { return this["Query1"] as QueryElement1; }
    }

    //query 2
    [ConfigurationProperty("Query2")]
    public QueryElement2 Query2
    {
        get { return this["Query2"] as QueryElement2; }
    }
}


public class QueryElement1 : ConfigurationElement
{
    public string Value { get; private set; }
    protected override void DeserializeElement(XmlReader reader, bool s)
    {
        Value = reader.ReadElementContentAs(typeof(string), null) as string;
    }        
}


public class QueryElement2 : ConfigurationElement
{
    public string Value { get; private set; }
    protected override void DeserializeElement(XmlReader reader, bool s)
    {
        Value = reader.ReadElementContentAs(typeof(string), null) as string;
    }
}

重写的DeserializedElement将反序列化QueryElement1&amp;的Xml(内部)。 2.

要从主应用程序中读取值,只需调用以下命令:

 //calling my query config
 QueryConfigurationSection wconfig = (QueryConfigurationSection)ConfigurationManager.GetSection("MyConfiguration/MyQuery");
string _query1 = wconfig.Query1.Value;
string _query2 = wconfig.Query2.Value; 

答案 3 :(得分:1)

也许你应该重新考虑你的设计。我只想把你想要的列表放在另一个文件而不是配置中。你可以做一个分隔的字符串,但如果列表变长,那么很难管理它。您可以将其放在文本文件或XML / JSON文件中。这里有一些代码可能是一个很好的起点。

public static class MyClass
    {
        private static string _path = ConfigurationManager.AppSettings["FilePath"];
        private static List<string> _list;

        static MyClass()
        {
            _list = new List<string>();
            foreach (string l in File.ReadAllLines(_path))
                _list.Add(l);
        }
        public static List<string> GetList()
        {
            return _list;
        }
    }

我把它变成了一个静态类,所以它只会从文件中读取一次,而不是每次需要从中获取信息时。

如果您需要更多功能,这也可能是放入数据库的好事。但是对于一个小的只读类,这对于更长的值来说比分隔的字符串更好。