I am trying to understand System.Configuration. I have been trying to use some examples of creating custom configuration sections in a VB.NET app. Not having much luck, so any help would be appreciated.
Thanks,
ed
Posted is the error message I get:
System.Configuration.ConfigurationErrorsException was unhandled
BareMessage="An error occurred creating the configuration section handler for DeleteGroup/DeleteSection: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"
Filename="C:\Devel_NET\Test_Custom_App_Config\Test_Custom_App_Config\bin\Debug\Test_Custom_App_Config.vshost.exe.Config"
Line=7
Message="An error occurred creating the configuration section handler for DeleteGroup/DeleteSection: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) (C:\Devel_NET\Test_Custom_App_Config\Test_Custom_App_Config\bin\Debug\Test_Custom_App_Config.vshost.exe.Config line 7)"
Source="System.Configuration"
StackTrace:
at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
at
The following is the code...
MAIN:
Imports System.Text
Module Module1
Sub Main()
Dim config As New Handler
config = CType(System.Configuration.ConfigurationManager.GetSection("DeleteGroup/DeleteSection"), Handler)
Dim sb As New StringBuilder
sb.Append("<h2>Attributes in the DeleteSection Element:</h2>")
sb.AppendFormat("Attrib1 = {0}<br/>", config.Attrib1.ToString())
sb.Append("<h2>Attributes in the Delete_Flatness Section Element:</h2>")
sb.AppendFormat("DaysOld = {0}<br/>", config.DaysOld.ToString())
sb.AppendFormat("SearchDir = {0}<br/>", config.SearchDir.ToString())
sb.AppendFormat("SearchString = {0}<br/>", config.SearchString.ToString())
Console.WriteLine(sb.ToString())
End Sub
End Module
CONFIG HANDLER CLASS:
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml
Public Class Handler
Inherits ConfigurationSection
Public Sub Handler()
End Sub
Public Sub Handler(ByVal attribVal As String)
Attrib1 = attribVal
End Sub
<ConfigurationProperty("Attrib1")> _
Public Property Attrib1() As String
Get
Return CStr(Me("Attrib1"))
End Get
Set(ByVal value As String)
Me("Attrib1") = value
End Set
End Property
<ConfigurationProperty("SearchDir")> _
Public Property SearchDir() As String
Get
Return CStr(Me("SearchDir"))
End Get
Set(ByVal value As String)
Me("SearchDir") = value
End Set
End Property
<ConfigurationProperty("SearchString")> _
Public Property SearchString() As String
Get
Return CStr(Me("SearchString"))
End Get
Set(ByVal value As String)
Me("SearchString") = value
End Set
End Property
<ConfigurationProperty("DaysOld")> _
Public Property DaysOld() As Integer
Get
Return CInt(Me("DaysOld"))
End Get
Set(ByVal value As Integer)
Me("DaysOld") = value
End Set
End Property
End Class
APP.CONFIG ENTRIES:
<configSections>
<sectionGroup name="DeleteGroup">
<section
name="DeleteSection"
type="Handler, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
allowLocation="true"
allowDefinition="Everywhere"/>
</sectionGroup>
</configSections>
<DeleteGroup>
<DeleteSection Attrib1="Flatness">
<Delete_Flatness
SearchDir="C:\N2CM_Flatdc"
SearchString="*.dat"
DaysOld="120"/>
</DeleteSection>
</DeleteGroup>