in

MySoftwareStartup

Dedicated to help those that own or are thinking of owning a software development company

Randy Walker Blog

Randy Walker's Blog website where he shares his experiences while being a software entrepreneur, Microsoft MVP, and industry influencer

Preferred method for read only Config files

I had come across an article where you can use the following line to read custom section settings from your config file (where SectionName is the name of your section) in .Net.

NameValueCollection myData = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection(SectionName);

The advantage of this is your setting doesn't have to be in the app.config file (web.config).  You can have a file named mywackyweirdfilename.config, and you use the exact same line.  The beauty of it is in the app.config (web.config) file where you point it to reference the mywackyweirdfilename.config.  e.g.

<configSections>
     <section name="SectionName" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<SectionName file="mywackyweirdfilename.config" />

Why is this such a beautiful way to handle your config file?  Because if you distribute your application, the system administrator can decide where to put the mywackyweirdfilename.config file.  It could, in fact, reside on a public share (although not recommended in case of the network being unavailable) or thumb drive.

So what's the problem?  You can't write to the config file easily.  I'll do a later post on my solution for writing to the config file, in which the format of the mywackyweirdfilename.config does not match below .

The format of mywackyweirdfilename.config for the examples above:

<?xml version="1.0" encoding="utf-8" ?>
<SectionName>
     <add key="myKey" value ="" />
</SectionName>

Technorati Tags: ,,

Comments

 

Randy Walker Blog said:

I&#39;ve been working with config files for quite some time. I was recently reminded that I needed to

June 5, 2008 12:25 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Randy Walker

Randy Walker is the owner and founder Harvest I.T. Inc., which is a Micro ISV that services the Wal-Mart vendor community and Retail Link. He has presented several sessions at various developer conferences on the subject of owning your own software company.
Copyright 2008 Randy Walker
Powered by Community Server (Commercial Edition), by Telligent Systems