在 C# 的配置文件中读取数组

read array in a config file in C#

到目前为止,我正在使用这样的配置文件来设置我的应用程序必须使用的环境:

<configuration>
  <appSettings>
    <add key="environment" value="demo"/>
  </appSettings>
</configuration>

我正在阅读如下:

AppSettingsSection appSettings = ConfigurationManager.OpenExeConfiguration( 
    (System.Reflection.Assembly.GetAssembly(typeof(LESTBackend))).Location).AppSettings;

从现在开始,我需要修改配置文件,以便让任何需要的人添加更多信息,例如hostport。我需要说我的应用程序连接到两个端点,所以使用旧的 xml 样式我需要这样的东西:

    <endpoints>
      <webapi host="hhh" port="1111"/>
      <authz host="hhh2" port="2222"/>
    </endpoints>

如何将此信息添加到我的配置文件中,以及如何读取它?

到目前为止,我已经能够从 类 创建以抽象我的配置部分和元素:

主要部分(BackendSection):

public class BackendSection : ConfigurationSection
{
    public const string BACKEND_ATTRIBUTE = "backend";

    [ConfigurationProperty(BackendSection.BACKEND_ATTRIBUTE, IsRequired = true)]
    public ScopeElement Scope
    {
        get
        {
            return (ScopeElement)this[BackendSection.BACKEND_ATTRIBUTE];
        }
        set
        {
            this[BackendSection.BACKEND_ATTRIBUTE] = value;
        }
    }
}

因此,每个 BackendSection 包含一个 ScopeElement。每个 ScopeElement 都有一个 scope 名称数组 EndpointElements:

public class ScopeElement : ConfigurationElement
{

    public const string SCOPE_ATTRIBUTE = "scope";
    public const string ENDPOINTS_ATTRIBUTE = "endpoints";
    public const string ENDPOINT_ATTRIBUTE = "endpoint";

    [ConfigurationProperty(ScopeElement.SCOPE_ATTRIBUTE, IsRequired = true)]
    public string Scope
    {
        get
        {
            return this[ScopeElement.SCOPE_ATTRIBUTE] as string;
        }
    }

    [ConfigurationProperty(ScopeElement.ENDPOINTS_ATTRIBUTE, IsRequired = false)]
    [ConfigurationCollection(typeof(EndpointsCollectionElements), AddItemName = ScopeElement.ENDPOINT_ATTRIBUTE)]
    public EndpointsCollectionElements Endpoints
    {
        get
        {
            return (EndpointsCollectionElements)this[ScopeElement.ENDPOINTS_ATTRIBUTE];
        }
    }

}

所以,

public class EndpointsCollectionElements : ConfigurationElementCollection
{
    public EndpointElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as EndpointElement;
        }

        set
        {
            if (base.BaseGet(index) != null)
                base.BaseRemoveAt(index);

            this.BaseAdd(index, value);
        }
    }

    public new EndpointElement this[string responseString]
    {
        get { return (EndpointElement)BaseGet(responseString); }
        set
        {
            if (this.BaseGet(responseString) != null)
                this.BaseRemoveAt(this.BaseIndexOf(this.BaseGet(responseString)));

            BaseAdd(value);
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((EndpointElement)element).App;
    }
}

EndpointElement

public class EndpointElement : ConfigurationElement
{

    public const string HOST_ATTRIBUTE = "host";
    public const string PORT_ATTRIBUTE = "port";
    public const string APP_ATTRIBUTE = "app";

    [ConfigurationProperty(EndpointElement.HOST_ATTRIBUTE, IsRequired = false)]
    public string Host
    {
        get
        {
            return this[EndpointElement.HOST_ATTRIBUTE] as string;
        }
    }

    [ConfigurationProperty(EndpointElement.PORT_ATTRIBUTE, IsRequired = false)]
    public int Port
    {
        get
        {
            return (int)this[EndpointElement.PORT_ATTRIBUTE];
        }
    }

    [ConfigurationProperty(EndpointElement.APP_ATTRIBUTE, IsRequired = false)]
    public int App
    {
        get
        {
            return (int)this[EndpointElement.APP_ATTRIBUTE];
        }
    }
}

谁能告诉我如何编写 .config 文件以及如何使用这些 类 读取它?

这个.config文件对吗?

<configuration>
  <configSections>
    <section name="backend" type="Backend.Implementations.Lest.Settings.BackendSection"
  </configSections>
  <backend scope="QA">
      <endpoints>
        <endpoint host="localhost" port="1111" app="webapi"/>
        <endpoint host="localhost" port="2222" app="authz"/>
      </endpoints>
    </backend>
</configuration>

您需要创建自定义配置部分。

MSDN:操作方法:How to: Custom Configuration Sections

顺便说一句,除非您为另一个可执行文件打开配置文件,否则您可以只使用 ConfigurationManager.AppSettings[""] 来引用当前正在执行的配置文件 AppSettings 部分。