5

I have created one class that directly map to ConfigSection of web. config. My class definition is given below :

public class myConfiguration: ConfigurationSection
{
    public myConfiguration()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    [ConfigurationProperty("fileName", IsRequired = true)]
    public string FileName
    {
        get { return this["fileName"] as string; }
    }


    [ConfigurationProperty("rootNode", IsRequired = true)]
    public string RootNode
    {
        get { return this["rootNode"] as string; }
    }

    [ConfigurationProperty("childNode", IsRequired = true)]
    public string ChildNode
    {
        get { return this["childNode"] as string; }
    }

    [ConfigurationProperty("comparableAttributes", IsRequired = true)]
    public string ComparableAttributes
    {
        get
        { return this["comparableAttributes"] as string; }
    }
}

I have created section in web.config file as below :

    <configSections>
    <section name="myConfigDemo" type="myConfiguration"/>
    </configSections>

Then i have used this section as

  <myConfigDemo fileName="myXml.xml" rootNode="world" childNode="country" comparableAttributes="id, population">

  </myConfigDemo>

Now the problem is How can I assign fileName = "anotherFile.xml" at runtime ? I have tried

   [ConfigurationProperty("fileName", IsRequired = true)]
    public string FileName
    {
        get { return this["fileName"] as string; }
        set {
            string str = this["fileName"] as string; 
              str  = value; }
    }

But my Visual Studio make my pc hang wen i use code above ! i Know the property is readonly when u use only get but set makes my pc hang !!! What can i do to change filename runtime ?

Chintan
  • 2,768
  • 4
  • 27
  • 43
  • http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-appsettings-at-runtime – stian.net Jan 23 '12 at 09:58
  • Two question with same title within one hour? You should edit your original question instead of creating a new one. http://stackoverflow.com/questions/8968924/how-to-modify-web-config-runtime (or modify the titles if they differ strongly) – Tim Schmelter Jan 23 '12 at 09:59
  • I think its about ` appSettings ` ! system configuration and i am eager about my own configuration ! As I know you can modify `appSettings` runtime but how to change my own configuration that i dont know! By d way thanks for suggestion ! – Chintan Jan 23 '12 at 10:01
  • 3
    Writing to the web.config is a bad idea. Every time it changes the web app will re-cycle! – Mad Pierre Jan 23 '12 at 14:03

1 Answers1

2

There are .net classes designed for more accurate accessing of almost everything that can be found in .config files (and not just appSettings or ConnectionStrings elements); documentation here: http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

I'm not sure if they provide ways to change values, though (take a look). However, a gotcha: config files are designed to configure the app at startup; in other words, the app reads the file when it starts, and then again if it's changed manually or by a process. With asp.net apps, this means that the application will automatically restart (by default; IIS setting).

If you really want to reconfigure the app at runtime, you'll force it to restart every time you SAVE the file. So, in that case, write code to make all changes in memory (e.g. by using xml classes), then save all at once.

There is a setting within app-pool to disable auto-restarting on config changes; however, if you do this, the app will NOT restart when you make config changes, and you'll have to write code to restart it for it to pick up those changes.

This class might be your friend, if you want to automatically serialize your custom config class to xml element: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

I hope that helps.