How to change the runtime of the web.config section?

I created one class that is directly displayed on ConfigSectionthe Internet. configurations. 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 created a section in the web.config file as shown below:

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

Then I 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 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 made my computer hang wen, I use the code above! I Know that a property is read only when you use it only get, but setit makes my computer freeze !!! What can I do to change the file runtime?

+5
source share
1 answer

.net, , .config( appSettings ConnectionStrings); : http://msdn.microsoft.com/en-us/library/x1et32w6.aspx

, , (). , gotcha: config ; , , , . asp.net , ( , IIS).

, , . , , (, xml), .

; , , , , , , .

, xml: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

, .

+2

All Articles