当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET ConfigurationSectionGroupCollection类代码示例

本文整理汇总了VB.NET中System.Configuration.ConfigurationSectionGroupCollection的典型用法代码示例。如果您正苦于以下问题:VB.NET ConfigurationSectionGroupCollection类的具体用法?VB.NET ConfigurationSectionGroupCollection怎么用?VB.NET ConfigurationSectionGroupCollection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ConfigurationSectionGroupCollection类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: UsingCustomSectionGroupCollection

' 导入命名空间
Imports System.Configuration
Imports System.Collections


' Define a custom section.
NotInheritable Public Class CustomSection
   Inherits ConfigurationSection
   
   
   Public Sub New()
   End Sub
   
    <ConfigurationProperty("fileName", _
    DefaultValue:="default.txt", _
    IsRequired:=True, _
    IsKey:=False), _
    StringValidator( _
    InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _
    MinLength:=1, _
    MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            Me("fileName") = value
        End Set
    End Property

    <ConfigurationProperty("maxUsers", _
    DefaultValue:=10000, _
    IsRequired:=False), _
    LongValidator(MinValue:=1, _
    MaxValue:=10000000, _
    ExcludeRange:=False)> _
   Public Property MaxUsers() As Long
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
            Me("maxUsers") = value
        End Set
    End Property

    <ConfigurationProperty("maxIdleTime", _
    DefaultValue:="0:10:0", _
    IsRequired:=False), _
    TimeSpanValidator(MinValueString:="0:0:30", _
    MaxValueString:="5:00:0", _
    ExcludeRange:=False)> _
    Public Property MaxIdleTime() As TimeSpan
        Get
            Return CType(Me("maxIdleTime"), TimeSpan)
        End Get
        Set(ByVal value As TimeSpan)
            Me("maxIdleTime") = value
        End Set
    End Property

End Class

' Define a custom section group.

NotInheritable Public Class CustomSectionGroup
   Inherits ConfigurationSectionGroup
   
   
   Public Sub New()
   End Sub

   
   Public ReadOnly Property Custom() As CustomSection
      Get
            Return CType(Sections.Get("CustomSection"), _
            CustomSection)
      End Get
    End Property

End Class


Class UsingCustomSectionGroupCollection

   
   ' Create a custom section group.
   Shared Sub CreateSectionGroup()
      Try
         
         Dim customSectionGroup As CustomSectionGroup
         
         ' Get the current configuration file.
            Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)
         
         ' Create the section group entry  
         ' in the <configSections> and the 
         ' related target section in <configuration>.
         If config.SectionGroups("CustomGroup") Is Nothing Then
            customSectionGroup = New CustomSectionGroup()
            config.SectionGroups.Add("CustomGroup", customSectionGroup)
            customSectionGroup.ForceDeclaration(True)
            config.Save(ConfigurationSaveMode.Full)
         End If
      
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
    
   
   ' Get the collection group keys i.e.,
   ' the group names.
    'Shared Sub GetAllKeys()

    '   Try
    '         Dim config _
    '         As System.Configuration.Configuration = _
    '         ConfigurationManager.OpenExeConfiguration( _
    '         ConfigurationUserLevel.None)

    '         Dim groups _
    '         As ConfigurationSectionGroupCollection = _
    '         config.SectionGroups

    '      Dim name As String
    '      For Each name In  groups.AllKeys
    '         Console.WriteLine("Key value: {0}", name)
    '      Next name


    '   Catch err As ConfigurationErrorsException
    '      Console.WriteLine(err.ToString())
    '   End Try
    'End Sub
   
   Shared Sub Clear()
      
      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)


         config.SectionGroups.Clear()
         
         config.Save(ConfigurationSaveMode.Full)
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
   

   Shared Sub GetGroup()
      
      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim customGroup _
            As ConfigurationSectionGroup = _
            groups.Get("CustomGroup")
         
         
         If customGroup Is Nothing Then
                Console.WriteLine( _
                "Failed to load CustomGroup.")
         Else
            ' Display section information
                Console.WriteLine("Name:       {0}", _
                customGroup.Name)
                Console.WriteLine("Type:   {0}", _
                customGroup.Type)
         End If
      
      
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
   

   Shared Sub GetEnumerator()
      
      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim groupEnum As IEnumerator = _
            groups.GetEnumerator()
         
         Dim i As Integer = 0
         While groupEnum.MoveNext()
            Dim groupName As String = groups.GetKey(i)
            Console.WriteLine("Group name: {0}", groupName)
            i += 1
         End While

      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
   
   
   ' Get the collection keys i.e., the
   ' group names.
   Shared Sub GetKeys()
      
      Try
            Dim config _
          As System.Configuration.Configuration = _
          ConfigurationManager.OpenExeConfiguration( _
          ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

         Dim key As String
         For Each key In  groups.Keys
            
            Console.WriteLine("Key value: {0}", key)
         Next key
      
      
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
      

   Shared Sub GetItems()
      
      Try
            Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim group1 As ConfigurationSectionGroup = _
            groups.Get("system.net")
         
            Dim group2 As ConfigurationSectionGroup = _
            groups.Get("system.web")
         
         
         Console.WriteLine("Group1: {0}", group1.Name)
         
         Console.WriteLine("Group2: {0}", group2.Name)
      
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub


   Shared Sub Remove()
      
      Try
         
            Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim customGroup _
            As ConfigurationSectionGroup = groups.Get("CustomGroup")
         
         If Not (customGroup Is Nothing) Then
            config.SectionGroups.Remove("CustomGroup")
            config.Save(ConfigurationSaveMode.Full)
         Else
                Console.WriteLine( _
                "CustomGroup does not exists.")
         End If
      
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
   
   
   
   ' Add custom section to the group.
   Shared Sub AddSection()
      Try
         
         Dim customSection As CustomSection
         
         ' Get the current configuration file.
            Dim config _
     As System.Configuration.Configuration = _
     ConfigurationManager.OpenExeConfiguration( _
     ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

         ' Create the section entry  
         ' in the <configSections> and the 
         ' related target section in <configuration>.
         Dim customGroup As ConfigurationSectionGroup
            customGroup = groups.Get("CustomGroup")
         
            If customGroup.Sections.Get( _
            "CustomSection") Is Nothing Then
                customSection = New CustomSection()
                customGroup.Sections.Add( _
                "CustomSection", customSection)
                customSection.SectionInformation.ForceSave = True
                config.Save(ConfigurationSaveMode.Full)
            End If
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub
   
   
   ' Exercise the collection.
   ' Uncomment the function you want to exercise.
    ' Start with CreateSectionGroup().
    Public Overloads Shared Sub Main(ByVal args() As String)
        CreateSectionGroup()
        AddSection()
        ' GetEnumerator();
        ' GetKeys();
        ' GetItems();
        ' Remove();
        ' Clear();

    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Configuration,代码行数:355,代码来源:ConfigurationSectionGroupCollection


注:本文中的System.Configuration.ConfigurationSectionGroupCollection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。