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

本文关键字:读取 数组 配置文件 | 更新日期: 2023-09-27 18:07:43

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

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

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

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

Main section (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;
    }
}

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

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

您需要创建一个自定义配置节。

有MSDN:如何:如何:自定义配置部分

作为题外话,除非您正在为另一个可执行文件打开配置文件,否则您可以使用ConfigurationManager.AppSettings[""]来引用当前正在执行的配置文件AppSettings节。