从.config文件中的自定义配置节组中检索密钥

时间:2017-10-16 10:48:23

标签: vb.net app-config

我有一个App.config文件,我在其中声明了一个部分组ListOfItems和两个部分Item_AItem_B

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
  <configSections>
    <sectionGroup name="ListOfItems">
      <section name="Item_A" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      <section name="Item_B" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
  </startup>
  <ListOfItems>
    <Item_A>
      <add key="FileAddress" value="http://www.example.com/file1.xml"/>
      <add key="FileType" value="xml"/>
      <add key="DateFieldFormat" value="yyyy/MM/dd"/>
      <add key="MainFolder" value="Folder_A"/>
      <add key="OutputFolder" value="output"/>
      <add key="ArchiveFolder" value="archive"/>
    </Item_A>
    <Item_B>
      <add key="FileAddress" value="http://www.example.com/file2.txt"/>
      <add key="FileType" value="txt"/>
      <add key="DateFieldFormat" value="yyyy-MM-dd"/>
      <add key="MainFolder" value="Folder_B"/>
      <add key="OutputFolder" value="output"/>
      <add key="ArchiveFolder" value="archive"/>
    </Item_B>
  </ListOfItems>
</configuration>

然后我定义了一个具有属性的类:

Namespace DataTypes
Friend Class ItemObject
    Inherits ConfigurationSection       

    <ConfigurationProperty("FileAddress", DefaultValue:=Nothing, IsRequired:=True)>
    Public Property FileAddressProperty As String
        Get
            Return CType(Me("FileAddress"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("FileAddress") = value
        End Set
    End Property

    <ConfigurationProperty("FileType", DefaultValue:="xml", IsRequired:=True)>
    Public Property FileTypeProperty As String
        Get
            Return CType(Me("FileType"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("FileType") = value
        End Set
    End Property

    <ConfigurationProperty("DateFieldFormat", DefaultValue:="yyyy/MM/dd", IsRequired:=True)>
    Public Property DateFieldFormatProperty As String
        Get
            Return CType(Me("DateFieldFormat"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("DateFieldFormat") = value
        End Set
    End Property

    <ConfigurationProperty("MainFolder", DefaultValue:="Folder", IsRequired:=True)>
    Public Property MainFolderProperty As String
        Get
            Return CType(Me("MainFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("MainFolder") = value
        End Set
    End Property

    <ConfigurationProperty("OutputFolder", DefaultValue:="output", IsRequired:=True)>
    Public Property OutputFolderProperty As String
        Get
            Return CType(Me("OutputFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("OutputFolder") = value
        End Set
    End Property

    <ConfigurationProperty("ArchiveFolder", DefaultValue:="archive", IsRequired:=True)>
    Public Property ArchiveFolderProperty As String
        Get
            Return CType(Me("ArchiveFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("ArchiveFolder") = value
        End Set
    End Property

End Class
End Namespace

现在我看了herehere,但我无法弄清楚如何编写以下循环: Foreach item in ListOfItems, create an ItemObject with data from App.config(我会将其添加到list ItemObject个。所以我会有一个包含2个项目的列表,一个用于Item_A,另一个用于{{1} }}

0 个答案:

没有答案