Click here to Skip to main content
12,896,953 members (51,577 online)
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads


88 bookmarked
Posted 19 Feb 2006

Custom Configuration Sections in .NET 2.0

, 19 Feb 2006
Creating a custom configuration section in .NET 2.0.
Imports System.Configuration
Public Class ConfigurationSample
   Inherits System.Configuration.ConfigurationSection

   Private m_cfg As Configuration

#Region " Instance methods "
   Public Sub Encript(ByVal encript As Boolean)
      If encript Then
      End If
   End Sub

   Public Sub Save()
      Me.SectionInformation.ForceSave = True
   End Sub

#End Region

#Region " Properties "

   <ConfigurationProperty("oneProperty", _
                          DefaultValue:="a Default Value", _
                          IsRequired:=True, IsKey:=True)> _
   Public Property OneProperty() As String
         Return CStr(Me("oneProperty"))
      End Get
      Set(ByVal value As String)
         Me("oneProperty") = value
      End Set
   End Property

   <ConfigurationProperty("oneNumericValue", DefaultValue:=55)> _
   Public Property OneNumericValue() As Integer
         Return CInt(Me("oneNumericValue"))
      End Get
      Set(ByVal value As Integer)
         Me("oneNumericValue") = value
      End Set
   End Property

   <ConfigurationProperty("oneElement", IsRequired:=True)> _
   Public ReadOnly Property OneElement() As ConfigurationSampleElementClass
         Return DirectCast(Me("oneElement"), _
      End Get
   End Property

#End Region

#Region " Shared methods "
   Private Const CS_SECTION As String = "sampleConfiguration"
   Private Shared m_current As ConfigurationSample

   Public Shared ReadOnly Property Current() As ConfigurationSample
         If m_current Is Nothing Then
            Dim cfg As Configuration
            Dim ctx As System.Web.HttpContext = System.Web.HttpContext.Current
            If ctx Is Nothing Then
               cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
               cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(ctx.Request.ApplicationPath)
            End If
            m_current = DirectCast(cfg.Sections(CS_SECTION), _
            m_current.m_cfg = cfg
         End If
            Return m_current
            'You could use this code if all that you need is to read the configuration.
            'Return DirectCast(ConfigurationManager.GetSection(CS_SECTION), _
            '                          ConfigurationSample)
      End Get
   End Property

#End Region

End Class

Public Class ConfigurationSampleElementClass
   Inherits ConfigurationElement

   <ConfigurationProperty("anotherProperty", IsRequired:=True)> _
   Public Property AnotherProperty() As String
         Return CStr(Me("anotherProperty"))
      End Get
      Set(ByVal value As String)
         Me("anotherProperty") = value
      End Set
   End Property
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Luis Angel R.C.
Web Developer
Spain Spain
My first computer was a MSX when I was twelve years old. I liked to develop simple games in Basic (you know... these old sprites...)
My first PC was a 8Mhz with two 5 1/4 floppy disks. The first language that I programed for it was assembler, and I liked to develop graphic demos. About that time Future Crew were my heros.

Now I have a computer engineer degree at Deusto University (Bilbao - Spain) since 1996, and I've been a Microsoft specialist since. Firstly with VB and now with VB.NET. In fact, I have three MCP Certifications (Win-VB6, Win-VB.NET and ASP.NET).

As a senior architect my work involves designing the application architecture for my team. For this work Rational XDE is, I think, the best tool ever made.

I like, development apart, travelling, watching movies, reading, and, of course, playing computer games, mainly MMOPRGs (currently WOW, and Vanguard:SoH as soon as it becomes available).

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 19 Feb 2006
Article Copyright 2006 by Luis Angel R.C.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid