Application Configuration Files in VC# (App.config > <app>.exe.config)

I am creating a application configuration file. I am interested in using custom sections, not just the default <appSettings> section. I have created handlers to read the custom section shown below. I want to update or write back to sections in the configuration file. How can I do this?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="TimeSpan">
<section
name="FromDateTime"
type="ConfigurationSectionObjects.DateTimeConfigHandler,AcuityTrendCharter"/>
<section
name="ToDateTime"
type="ConfigurationSectionObjects.DateTimeConfigHandler,AcuityTrendCharter"/>
</sectionGroup>
<section
name="DateTimeOfLastBackup"
type="ConfigurationSectionObjects.DateTimeConfigHandler,AcuityTrendCharter"/>
</configSections>
<TimeSpan>
<FromDateTime>
<Year>2005</Year>
<Month>8</Month>
<Day>1</Day>
<Hour>12</Hour>
<Minute>30</Minute>
</FromDateTime>
<ToDateTime>
<Year>2005</Year>
<Month>8</Month>
<Day>3</Day>
<Hour>12</Hour>
<Minute>30</Minute>
</ToDateTime>
</TimeSpan>
<DateTimeOfLastBackup>
<Year>2005</Year>
<Month>8</Month>
<Day>1</Day>
<Hour>12</Hour>
<Minute>30</Minute>
</DateTimeOfLastBackup>
<appSettings>
<add key="BackupDirectory" value=""/>
<add key="DatabaseDirectory" value="c:\\"/>
<add key="DatabaseFileName" value="transaction.mdb"/>
</appSettings>
</configuration>

All of the values above for TimeSpan, DateTimeOfLastBackup and all the values described in the <appSettings></appSettings> will require updating. I can successfully read the values in the sections above, but I do not know the direction to go on updating these values from the application.

Shown below, I additionally want to read these sections which have yet to be defined in the <configSections></configSections> section. I have a <TrendColors></TrendColors> section that has node-list of color elements with attributes. I also have a <Views></Views> section that has subsections, node list <ChildForm></ChildForm>, each ChildForm section having one attribute. And, each ChildForm section has Trend elements with multiple attibutes. How should I read these? None of the section-elements nor property-attributes below require updating from the application at this time.

</configuration>
<!-- List of Trend Colors used to plot points and trend lines. -->
<!-- Colors for additional trends should be added as needed. -->
<TrendColors>
<Color
Name="Red"
ID="0xFF0000"/>
<Color
Name="Green"
ID="0x00FF00"/>
<Color
Name="Blue"
ID="0x0000FF"/>
<Color
Name="Red/Blue"
ID="0xFF00FF"/>
<Color
Name="Red/Green"
ID="0xFFFF00"/>
<Color
Name="Green/Blue"
ID="0x00FFFF"/>
<Color
Name="Grey"
ID="0x808080"/>
</TrendColors>
<!-- List of Views each with corresponding trends. -->
<Views>
<ChildForm Name="View1">
<Trend
Name="Trend1"
ID="0001"/>
<Trend
Name="Trend2"
ID="0002"/>
<Trend
Name="Trend3"
ID="0003"/>
</ChildForm>
<ChildForm Name="View2">
<Trend
Name="Trend4"
ID="0004"/>
<Trend
Name="Trend5"
ID="0005"/>
<Trend
Name="Trend6"
ID="0006"/>
</ChildForm>
<ChildForm Name="View3">
<Trend
Name="Trend7"
ID="0007"/>
<Trend
Name="Trend8"
ID="0008"/>
<Trend
Name="Trend9"
ID="0009"/>
</ChildForm>
<ChildForm Name="View4">
<Trend
Name="Trend10"
ID="0010"/>
<Trend
Name="Trend11"
ID="0011"/>
<Trend
Name="Trend12"
ID="0012"/>
</ChildForm>
<ChildForm Name="View5">
<Trend
Name="Trend13"
ID="0013"/>
<Trend
Name="Trend14"
ID="0014"/>
<Trend
Name="Trend15"
ID="0015"/>
</ChildForm>
<ChildForm Name="View6">
<Trend
Name="Trend16"
ID="0016"/>
<Trend
Name="Trend17"
ID="0017"/>
<Trend
Name="Trend18"
ID="0018"/>
</ChildForm>
<ChildForm Name="View7">
<Trend
Name="Trend19"
ID="0019"/>
<Trend
Name="Trend20"
ID="0020"/>
<Trend
Name="Trend21"
ID="0021"/>
</ChildForm>
<ChildForm Name="View8">
<Trend
Name="Trend22"
ID="0022"/>
<Trend
Name="Trend23"
ID="0023"/>
<Trend
Name="Trend24"
ID="0024"/>
</ChildForm>
<ChildForm Name="View9">
<Trend
Name="Trend25"
ID="0025"/>
<Trend
Name="Trend26"
ID="0026"/>
<Trend
Name="Trend27"
ID="0027"/>
</ChildForm>
<ChildForm Name="View10">
<Trend
Name="Trend28"
ID="0028"/>
<Trend
Name="Trend29"
ID="0029"/>
<Trend
Name="Trend30"
ID="0030"/>
</ChildForm>
</Views>
</configuration>

Any advise would be helpful.

James

[5914 byte] By [jmsigler2] at [2007-12-16]
# 1
If you're using .NET Framework 2.0, you can leverage the Configuration API to modify your configuration settings. This is definitely the way forward.

If you're looking for a solution using today's technologies, take a look at Enterprise Library and specifically the Configuration Management app block as it allows you to read and write configuration sections:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib.asp

JamesKovacs at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Your suggestion looks like a good direction; however, I must conform to .NET Framework 1.1 for now. Although I cannot find support for writing to Application Configuration files, I find it odd that this does not exist in the design pattern which is a newer technology than private INI files of the past.

I am wondering if I have to use a more standard XML file approach such as opening an XML file and using a XML writer class of some nature. However, again, this seems like an indirect approach.

James

jmsigler2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
look at this thread
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=66282
gibic at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
It was a notable omission from .NET Framework 1.X, which is why they've added it for 2.0. The Configuration Management app block that I mentioned runs on .NET Framework 1.1 and allows you to read/write XML config files. I would recommend that approach. You can also go the XmlWriter route, but that's more work IMHO.
JamesKovacs at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
Is there any special consideration when it comes to Windows Server 2003? On any XP machine, [Application].exe.config works correctly, however, on every Windows Server 2003 machine I use, the file must be named [Application].config in order for the application to read the configuration.

I have performed an extensive search about this issue, but can't find anything. Thank you.

timlajaunie at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
It is hard to believe that no one else has ever come across this issue. The documentation for the ConfigurationManager class states that it is supported on Windows Server 2003.
timlajaunie at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...