Fog Creek Software
Discussion Board




Help with System.Configuration Namespace

Here's the scenario:

We have a web app and I would like to store application configuration info (such as caching flags, db connection string, etc) in an xml configuration file.  We the app loads (Application_onStart event), read the config and set it in my config class.

I believe there is some way to read the config file using the System.Configuration namespace, cast it to a NameValueCollection type so that I can traverse through the xml looking for my config elements.  However, I can't get the config file to be read.

Does anyone have any ideas or links for me?  I've scoured MSDN, examined a Duwamish sample that uses this method, google my arse off but I am still at square one.

Any help would be really appreciated.  I am a lone developer so I have no one to bounce these types of ideas off of.

shiggins
Wednesday, November 12, 2003

"We the app loads" should read "When the app loads".  Oops;)

shiggins
Wednesday, November 12, 2003

System.Configuration is used to read the application configuration file. For executables, that's appname.exe.config; for web applications, that's web.config.

If you want to read an arbitrary XML file, you should use the XML APIs.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, November 12, 2003

You can store settings in web.config
<configuration>
  <configSections>
      <appSettings>
        <add key="key" value="value">
        ...
    
In code use something like this to read the values.

using System.Configuration;
...
  string value = ConfigurationSettings.AppSettings["key"];


See: http://msdn.microsoft.com/library/en-us/cpgenref/html/gngrfappsettingselement.asp and http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemconfigurationconfigurationsettingsclassappsettingstopic.asp


If reading from a custom file use something like:

string myConfigFile = HttpContext.Current.Server.MapPath("~/MyCustomConfig.xml");
anXmlDocument.Load(myConfigFile);
string value = anXmlDocument.SelectSingleNode("/Config/Setting[@Key='ConnectionString']/@Value").Value;

Duncan Smart
Wednesday, November 12, 2003

If you're a lone developer then you'd better get used to using the resources that are just a few mouse clicks away too:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconaspnetconfiguration.asp

All this stuff is on your hard drive too in the .NET Framework SDK Documentation.

Duncan Smart
Wednesday, November 12, 2003

Thanks all.  I had the code correct except I was calling my config file app.config not web.config.

shiggins
Wednesday, November 12, 2003

*  Recent Topics

*  Fog Creek Home