Create and Use a Custom Config File
page 4 of 7
by Steven Archibald
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43516/ 82

Create a Static Utility Method to Extract the Values

I have a utility class, cleverly named Utility.cs, that is present in all my websites. I’ll present a sample as though this were the only method in the class (don’t want to give away too many secrets all at once, eh?). In my example, I exclude the constructor and remove some tabs to conserve valuable web page space and protect the environment.

Listing 4 – The utility method

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;


namespace MyWebSite
{
public class Utility
{
      public static string GetCustomConfigValue(string SectionName,string KeyName)
      {
  NameValueCollection myData = (NameValueCollection)
  System.Configuration.ConfigurationSettings.


  GetConfig(SectionName);


    if (myData == null)
   {
    throw(new NullReferenceException("Section Name not found: " + SectionName));
            }
            string myValue = myData.Get(KeyName);


            if (myValue == null)
            {
                  throw(new NullReferenceException("Key Name not found: " + KeyName));
            }
            return myValue;
      }
}

Note that we are raising exceptions here. It’s recommended, but you could just return a value like “Key name not found” so you present some value that indicates to you (when you are testing) that something didn’t work. Note the System namespaces imported at the top of the file. You need them.

Now, we go get those valuable little puppies of information!


View Entire Article

User Comments

Title: Thanks   
Name: Rauf
Date: 2009-12-22 7:03:28 AM
Comment:
Very useful article. keep the good work going
Cheers!!
Title: Thanks   
Name: Fawad
Date: 2009-11-20 10:22:09 AM
Comment:
Thanks buddy, loved the article. Here is a VB counterpart if anyone needs it:
===========================================================
********* Get Config Values ******************************
===========================================================
Public Shared Function GetConfigValue(ByVal strSectionName As String, ByVal strKeyName As String) As String

Dim temp As String = Nothing

Dim myData As NameValueCollection = DirectCast(ConfigurationManager.GetSection(strSectionName), _
NameValueCollection)

If myData Is Nothing Then
'Section name not found
Throw New NullReferenceException("Section name not found:" & strSectionName)
Else
'section found, try to get the value
temp = myData.Get(strKeyName)

If temp Is Nothing Then
'Key name not found, throw new exception
Throw New NullReferenceException("Key name not found: " & strKeyName)
End If
End If

End Function

===========================================================
**** Write Config Values (NOT TESTED BUT SHOULD WORK) ****
===========================================================

Public Shared Function UpdateConfigValue(ByVal strConfigFilePath As String, ByVal strSectionName As String, _
ByVal strKeyName As String, ByVal strValue As String) As Boolean

Dim xmlDoc As New XmlDocument
Dim flag As Boolean = False

xmlDoc.Load(strConfigFilePath)

For Each node As XmlNode In xmlDoc.Item(strSectionName)

'Look for add attribute
If node.Name = "add" Then

'see if we have specified key in config file
If node.Attributes.GetNamedItem("key").Value = strKeyName Then

'Key found, upd
Title: value not updated   
Name: monaung
Date: 2009-05-10 2:44:36 AM
Comment:
if i am chaging the value in appReleaseSection in Cutom.Config file it is not reflecting. it gives me previous entered value why?
please can you mail me back how can i do this?
mon.aung@gmail.com
Title: config file not web.config/app.config   
Name: sixYo
Date: 2009-04-07 12:51:29 AM
Comment:
you can store the data in a xml file without web.config / app.config

http://www.codeproject.com/KB/aspnet/CustomXmlConfigForAspNet.aspx
Title: Little Change   
Name: ACostaF
Date: 2008-04-18 9:51:47 AM
Comment:
Good article. In order to MSDN Library suggestion, you must use System.Configuration.ConfigurationManager.GetSection instead of System.Configuration.ConfigurationSettings.GetConfig as ConfigurationSettings is obsolete.
Title: Need your help   
Name: Sukki
Date: 2008-03-01 12:17:10 PM
Comment:
it works well for me but when i try to add a subsection it fails. How can i handle this
Title: Good One   
Name: Rajesh
Date: 2007-07-24 6:16:13 AM
Comment:
Thanks Steven Archibald, this article was good and helped me a lot.

In addition to this, i wanted to use Release.xml file rather than Release.config file. Is the code works same for the .xml file?

Anticipating your response.

Rajesh
Title: Thank you!   
Name: Chris Bidwill
Date: 2007-05-23 10:27:30 PM
Comment:
Thanks, this was a huge help. From all the other posts I've found out there it looked like creating custom sections was a huge pain (so much it was easier just to parse the xml doc). This saved me a lot of frustration.
Title: Good Work   
Name: Satyam Kumar
Date: 2006-10-18 3:04:39 PM
Comment:
Thanks to aspalliance team.

Nice work done by team that i was looking for long time.

If any addition or Enhancement then pls let me know.

Satyam
satyam@rategain.com
Title: Custom.Config File   
Name: Satyadeep Behera
Date: 2006-06-16 4:57:48 AM
Comment:
after updating the Custom.config file can i able to get the
new values that has been saved at run time, i have dataaccess layer and i want to fix this value here.
please help me.
Title: Custom.Config File   
Name: Satyadeep Behera
Date: 2006-06-15 3:21:08 AM
Comment:
but is if i am chaging the value in appReleaseSection in Cutom.Config file it is not reflecting it gives me previous entered value why?
please can you mail me back how can i do this?
bsatyadeep@zilog.com






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 5:24:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search