Dave,

Its basically just an XML file. Unfortunately the configuration
architecture in .NET doesn't support writing to the configuration
file with the same ease as reading. Basically you need to open
it with an XmlDocument then work with the DOM. Here is a minmal
example of changing an application setting.

using ConfWrite;
using System;
using System.Configuration;
using System.Xml;

namespace ConfWrite
{

        public class EntryPoint
        {

                public static void Main()
                {

                        // Declare locals.
                        XmlDocument configFile = null;
                        XmlElement configuration = null;
                        XmlElement mySetting = null;

                        // Load up the application config file.
                        configFile = new XmlDocument();
                        configFile.Load("ConfWrite.exe.config");

                        // Grab the <configuration /> element, well,
atleast
                        // it should be if it a valid .NET config file.
                        configuration = configFile.DocumentElement;

                        // Grab a particular appSetting with the key of
                        // appSetting - pretty straight-forward.
                        mySetting =
(XmlElement)configuration.SelectSingleNode("appSettings/add[@key='MySett
ing']");

                        // Print out the before value.
                        Console.WriteLine(
                                "Before: {0}",
                                mySetting.Attributes["value"].Value
                                );

                        // Update the value with the current date.
                        mySetting.Attributes["value"].Value =
DateTime.Now.ToString();

                        // Print out the after value.
                        Console.WriteLine(
                                "Before: {0}",
                                mySetting.Attributes["value"].Value
                                );

                        // Save the current configuration back to disk.
                        configFile.Save("ConfWrite.exe.config");

                }

        }

}

When you implement IConfigurationSectionHandler you get
passed an XmlNode instance, but if you grab the document
reference from that and try and persist it to disk you
end up overriting the config file with just that element
-- disaster for anyone who attempts it and doesn't back-up
first. The other thing you may run into is a file locking
issue where the configuration file gets locked during the
process of a read so you need to space writes out from
the reads (if using the default mechanism).

There is probably a market for a two way configuration
reading library which can support IConfigurationSectionHandler
interface but also adds two more:

        IConfigurationSectionWriter
        IConfigurationSectionReader

Maybe Microsoft might implement something like this in
future versions of the .NET Framework.

----------------------------------------
- Mitch Denny
- [EMAIL PROTECTED]
- +61 (414) 610-141
-

-----Original Message-----
From: The DOTNET list will be retired 7/1/02
[mailto:[EMAIL PROTECTED]] On Behalf Of dave wanta
Sent: Sunday, 16 June 2002 08:59
To: [EMAIL PROTECTED]
Subject: [DOTNET] web.config


hi all,
Does anyone know of any urls\examples for programmatically modifying the
web.config?

Thanks,
Dave

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to