Jasinski Technical Wiki


Home Page
All Pages

Quick Search
Advanced Search »

Contributor Links

Create a new Page
File Management
Your Profile

Other Wiki Sections



Page History: ConfigMgr Class

Compare Page Revisions

« Older Revision - Back to Page History - Current Revision

Page Revision: Fri, Jun 24, 2011, 2:57 PM

This page is part of the Class Library Pages collection.
Click the icon to see the index.
This class depends on the Parser class, found here.


The ConfigMgr class provides a means of working with application settings found in either an app.config or a web.config file.


  1. Copy the source code, found below
  2. Modify the Key enum to suit your requirements
  3. Modify the static constructor to fill the _dict dictionary.

Source Code


This code is deprecated by the C# code below.
Imports Microsoft.VisualBasic
Imports System.Collections.Generic

Public Class ConfigMgr

    Public Enum Key
    End Enum

    Private Shared _dict As Dictionary(Of Key, String)

    Shared Sub New()

        _dict = New Dictionary(Of Key, String)()
        _dict.Add(Key.AppId, "ApplicationID")
        _dict.Add(Key.LinesPerPage, "LinesPerPage")
        _dict.Add(Key.SingleSignOnMode, "SSO")

    End Sub
    Public Shared Function GetKey(ByVal key As Key) As String

        Return _dict(key)

    End Function
    Public Shared Function GetValue(ByVal key As Key, Optional ByVal defaultValue As String = "") _
    As String

        Dim s As String = ConfigurationManager.AppSettings(_dict(key))

        If s Is Nothing Then
            s = ""
        End If

        If s.Length = 0 Then
            s = defaultValue
        End If

        Return s

    End Function
    Public Shared Function TryGetValue(Of T)(ByVal key As Key, ByRef value As T) As Boolean

        Return Parser(Of T).TryParse(GetValue(key), value)

    End Function
End Class


public enum ConfigKey
/* Elements of this enum should match the entries in your web.config file's 
   /configuration/appSettings section */

public static class Config
    public static string Get(ConfigKey key)
        string s = ConfigurationManager.AppSettings[key.ToString()];

        if (s == null)
            throw new Exception("Configuration setting [" + key.ToString() + "] not found.");
        return s;
    public static T Get<T>(ConfigKey key)
        T result = default(T);
        string s = Get(key);
        string t = typeof(T).ToString();

        if (!Parser<T>.TryParse(s, out result))
            throw new Exception("Couldn't parse configured value, '" + s + "', for key [" + 
                key.ToString() + "] as a(n) " + t);

        return result;

ScrewTurn Wiki version Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2021, Patrick Jasinski.