I have having trouble to get the following code (related to custom configuration .net 2.0) to work. Can anyone please help?
Custom config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Config" type="LibRefresh.Config, LibRefresh" />
<section name="WebApplicationLists" type="LibRefresh.CustomConfigSection, LibRefresh" />
</configSections>
<Config>
<WebApplicationLists>
<WebApplication Name="AUM" URL="http://ntsvgss15120b/aum/default.aspx" LastUpdateOn="10/15/2007 11:47:27 AM" RefreshInterval="20" />
<WebApplication Name="BDRecon" URL="http://ntsvgss15120b/bdrecon/default.aspx" LastUpdateOn="10/12/2007 2:24:41 PM" RefreshInterval="20" />
</WebApplicationLists>
</Config>
</configuration>
Custom config class:
Imports System.Configuration
Public Class CustomConfigSection
Inherits System.Configuration.ConfigurationSection
Private m_cfg As Configuration
#Region " Instance methods "
'Public Sub Encript(ByVal encript As Boolean)
' If encript Then
' Me.SectionInformation.ProtectSection("DataProtecti onConfigurationProvider")
' Else
' Me.SectionInformation.UnprotectSection()
' End If
' Me.Save()
'End Sub
Public Sub Save()
Me.SectionInformation.ForceSave = True
m_cfg.Save(ConfigurationSaveMode.Full)
End Sub
#End Region
<ConfigurationProperty("WebApplicationLists")> _
Public Property WebApplicationLists() As WebApplicationCollection
Get
Return TryCast(Me("WebApplicationLists"), WebApplicationCollection)
End Get
Set(ByVal value As WebApplicationCollection)
Me("WebApplicationLists") = value
End Set
End Property
#Region " Shared methods "
Private Const CS_SECTION As String = "WebApplicationLists"
Private Shared m_current As CustomConfigSection
Public Shared ReadOnly Property Current() As CustomConfigSection
Get
If m_current Is Nothing Then
Dim cfg As Configuration
Dim fileMap As New ExeConfigurationFileMap()
fileMap.ExeConfigFilename = "c:\custom.config"
cfg = ConfigurationManager.OpenMappedExeConfiguration(fi leMap, ConfigurationUserLevel.None)
m_current = DirectCast(cfg.Sections(CS_SECTION), _
CustomConfigSection)
m_current.m_cfg = cfg
End If
Return m_current
End Get
End Property
#End Region
End Class
Public Class WebApplication : Inherits ConfigurationElement
Sub New()
End Sub
<ConfigurationProperty("Name", IsRequired:=True)> _
Public Property Name() As String
Get
Return TryCast(Me("Name"), String)
End Get
Set(ByVal value As String)
Me("Name") = value
End Set
End Property
<ConfigurationProperty("URL", IsRequired:=True)> _
Public Property URL() As String
Get
Return TryCast(Me("URL"), String)
End Get
Set(ByVal value As String)
Me("URL") = value
End Set
End Property
<ConfigurationProperty("LastUpdateOn", IsRequired:=True)> _
Public Property LastUpdateOn() As String
Get
Return TryCast(Me("LastUpdateOn"), String)
End Get
Set(ByVal value As String)
Me("LastUpdateOn") = value
End Set
End Property
<ConfigurationProperty("RefreshInterval", IsRequired:=True)> _
Public Property RefreshInterval() As String
Get
Return TryCast(Me("RefreshInterval"), String)
End Get
Set(ByVal value As String)
Me("RefreshInterval") = value
End Set
End Property
End Class
Public Class WebApplicationCollection : Inherits ConfigurationElementCollection
Protected Sub New()
End Sub
Protected Sub New(ByVal comparer As IComparer)
MyBase.New(comparer)
End Sub
Public Overrides ReadOnly Property CollectionType() As ConfigurationElementCollectionType
Get
Return ConfigurationElementCollectionType.BasicMap
End Get
End Property
Protected Overrides ReadOnly Property ElementName() As String
Get
Return "WebApplication"
End Get
End Property
Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
Return New WebApplication
End Function
Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
Return TryCast(element, WebApplication).Name
End Function
Default Public Overloads Property Item(ByVal index As Integer) As WebApplication
Get
Return CType(MyBase.BaseGet(index), WebApplication)
End Get
Set(ByVal value As WebApplication)
If Not (MyBase.BaseGet(index) Is Nothing) Then
MyBase.BaseRemoveAt(index)
End If
MyBase.BaseAdd(index, value)
End Set
End Property
Default Public Overloads ReadOnly Property Item(ByVal name As String) As WebApplication
Get
Return TryCast(MyBase.BaseGet(name), WebApplication)
End Get
End Property
Public Sub Add(ByVal element As WebApplication)
MyBase.BaseAdd(element)
End Sub 'Add
Public Sub Remove(ByVal name As String)
MyBase.BaseRemove(name)
End Sub 'Remove
Public Sub Remove(ByVal element As WebApplication)
MyBase.BaseRemove(GetElementKey(element))
End Sub 'Remove
Public Sub Clear()
MyBase.BaseClear()
End Sub 'Clear
Public Sub RemoveAt(ByVal index As Integer)
MyBase.BaseRemoveAt(index)
End Sub 'RemoveAt
Public Function GetKey(ByVal index As Integer) As String
Return CStr(MyBase.BaseGetKey(index))
End Function 'GetKey
End Class