I am sure that most of you have needed to use the App.Config file
before to put different sort of startup/init data before. And I am
also sure that most of you have wanted to create your own structure for
the file, but have resorted to using the built in <appSettings>
node and retrieved the value in code using
ConfigurationManager.AppSettings[ "MyKey" ].
Well, I have for
a long time wanted to figure out how to use the built in
ConfigurationSection class in order to describe and load my own data
format. After a few hours of trial and error along with googling for
help I have finaly figured out how do create my own data structure and
use it within my application.
In order to load this information in from the App.Config file there are several classes taht are needed.
- ConfigurationSection -- This is the returned object that will hold onto your custom element
- ConfigurationElementCollection-- This is the element that you create the describes your collection of data
- ConfigurationElement -- This is the element that you create that will describe your object/entity for the data.
Getting Started.
You
first need to create your app.Config file if you don't already have
that. After you have your app.config file created, open it up and
copy/paste the following lines of code between the
<configuration> nodes
This is the configuartion section of the app.config file. It is here you setup/create your custom section definition.
The name of the section will need to match the name of the custom section settings that will be created below. The type is the type of object that will hold onto the custom settings. The first part is the full path (with namespace) of the class the second is the name of the assembly.
<configSections>
<!--
name = This needs to match the name of the section that the settings are stored
type = is a 2 part value. Part 1 is the full path (w/ namespace to the class that will hold
this data. Part 2 is the name of the assembly this class is found in.
-->
<section name="StartupFolders" type="ConfigSectionTester.StartupFoldersConfigSection, ConfigSectionTester"/>
</configSections>
Next
you will need to create your custom section that will hold your data.
copy/paste the following lines of code between the
<configuration> nodes, but after the <configSections> node
you created above.
This is the custom data section that will be implemented via your custom data model.
<StartupFolders>
<Folders>
<add folderType="A" path="c:\foo" />
<add folderType="B" path="C:\foo1" />
</Folders>
</StartupFolders>
Now
that your app.config file is created lets move the the class files you
will need that will store and interact with the app.config file.
First
you will need to create your ConfigurationSection class that will hold
onto the data that was retrieved from the app.config file
This s the custom section handler that is needed in order to get the process rolling. As you can see this inherits from ConfigurationSection which allows us to be able to populate this from the ConfigurationManager at runtime.
The ConfigurationProperty( "Folders" ) needs to match the name of the root node that will contain all our your data items (see above for example).
The FoldersCollection class name will be created later and is the collection that will hold each of your custom config elements.
/// <summary>
/// The Class that will have the XML config file data loaded into it via the configuration Manager.
/// </summary>
public class StartupFoldersConfigSection : ConfigurationSection
{
/// <summary>
/// The value of the property here "Folders" needs to match that of the config file section
/// </summary>
[ConfigurationProperty( "Folders" )]
public FoldersCollection FolderItems
{
get { return ( (FoldersCollection)( base[ "Folders" ] ) ); }
}
}
Next you will need to create the ConfigurationElementCollection class that will hold onto the collection of custom elements.
Here is the collection class that holds onto each of the custom elements that are described in the app.config class.
/// <summary>
/// The collection class that will store the list of each element/item that
/// is returned back from the configuration manager.
/// </summary>
[ConfigurationCollection( typeof( FolderElement ) )]
public class FoldersCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FolderElement();
}
protected override object GetElementKey( ConfigurationElement element )
{
return ( (FolderElement)( element ) ).FolderType;
}
public FolderElement this[int idx ]
{
get
{
return (FolderElement) BaseGet(idx);
}
}
}
Finally
you will need to create your ConfigurationElement class that will hold
and describe your custom data from the app.config file.
This is the class that will hold onto the data for your custom section element in the app.config file. The ConfigurationProperty("folderType") needs to map to the xml attribute name in the app.config file for each of the elements you wish to use. The other values such as DefaultValue="", IsKey=true, IsRequired=true are here only to show the different options that are available to you.
/// <summary>
/// The class that holds onto each element returned by the configuration manager.
/// </summary>
public class FolderElement : ConfigurationElement
{
[ConfigurationProperty("folderType", DefaultValue="", IsKey=true, IsRequired=true)]
public string FolderType
{
get
{
return ((string) (base["folderType"]));
}
set
{
base["folderType"] = value;
}
}
[ConfigurationProperty( "path", DefaultValue = "", IsKey = false, IsRequired = false )]
public string Path
{
get
{
return ( (string)( base[ "path" ] ) );
}
set
{
base[ "path" ] = value;
}
}
}
Now
that we have all the data and classes setup, here is how you actually
get the data out of the app.config file and into your custom data model.
NOTE: You will need to reference the System.Configuration namespace in order for this to work.
StartupFoldersConfigSection section = (StartupFoldersConfigSection)ConfigurationManager.GetSection( "StartupFolders" );
if ( section != null )
{
System.Diagnostics.Debug.WriteLine( section.FolderItems[ 0 ].FolderType );
System.Diagnostics.Debug.WriteLine( section.FolderItems[ 0 ].Path );
}
Well,
here you have it. A complete example on how to create a custom
configuration section that can be read into your own custom data model.
Posted
11-13-2006 6:24 AM
by
Derik Whittaker