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

发布于 2024-10-03 00:17:44 字数 493 浏览 0 评论 0原文

我正在学习如何使用配置文件,遇到了一些问题,希望这里有人能给我一些建议。我的文件是否是 XML 并不重要,但我读过的大多数示例都在使用它们,我全力以赴,让我的生活变得更轻松。

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

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

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

再说一次,如果这是 XML、平面文件等都没关系……我只是想学习如何使用配置文件而不是直接将值硬编码到应用程序中。

感谢您的帮助。

I'm learning about how to use config files and I ran into some problems that I'm hoping someone here can give me some advice. It doesn't matter if my files are XML or not but the majority of examples I have read are using them and Im all for anything that makes my life easier.

the problem Im running into is that the appSettings file seems to be setup to only accept one value for one key and I would like to have something similar to:

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

Ive found a hack here but it was written over 6 years ago and I didn't know if there was a better way.

Again, it doesnt matter if this is XML, a flat file, etc.... Im just trying to learn how to use config files instead of hard coding values directly into the app.

Thanks for your help.

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

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

发布评论

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

评论(4

你好,陌生人 2024-10-10 00:17:44

如果您确实需要在密钥下存储多台机器,那么这样做会更合适:

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

分隔符是您选择的字符。

if you really need to store multiple machines under the key, it would be more appropriate to do:

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

with the delimiter being a character of your choosing.

﹂绝世的画 2024-10-10 00:17:44

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

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

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

An alternative to entry attributes would be to add child nodes to your setting node:

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

This way you don't need string manipulations to extract the different values.

青瓷清茶倾城歌 2024-10-10 00:17:44

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

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

之后添加,您就可以在 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 和 Xml(内部)。 2.

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

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

You can make use of configuration sections where you can define your own configuration. Just add

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

after the <configuration> and you can add your custom section just after the appsetting

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

To read you need to create few classes

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

The overridden DeserializedElement will deserialize the Xml(inside) the QueryElement1 & 2.

To read the values from the main application, you just need to call the following:

 //calling my query config
 QueryConfigurationSection wconfig = (QueryConfigurationSection)ConfigurationManager.GetSection("MyConfiguration/MyQuery");
string _query1 = wconfig.Query1.Value;
string _query2 = wconfig.Query2.Value; 
一世旳自豪 2024-10-10 00:17:44

也许你应该重新考虑你的设计。我只是将您想要的列表放在另一个文件中,而不是配置中。您可以使用分隔字符串,但如果列表很长,则很难管理它。您可以将其放入文本文件或 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;
        }
    }

我将其设置为静态类,因此它只会从文件中读取一次,而不是每次您需要从中获取信息时。

如果您需要更多功能,那么将其放入数据库中也可能是一件好事。但对于小型只读类型,这比使用较长值的分隔字符串效果更好。

Maybe you should rethink your design. I would just put the list you want in another file and not the config. You could do a delimited string but then if the list got long it would be hard to manage it. You could just put it in a text file or an XML/JSON file. Here is some code that might be a good place to start.

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

I made it a static class so it would only read from the file once and not everytime you need to get information from it.

This might also be a good thing to put in a database if you need more functionality. But for a small read-only kind of thing, this will work better than a delimited string for longer values.

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