First we need to create a custom ConfigurationElement. This configuration element only has a value attribute.

public class MyConfigurationElement : ConfigurationElement

{

/// <summary>

/// Initializes a new instance of the <see cref="ValueConfigurationElement"/> class.

/// </summary>

public MyConfigurationElement ()

{}

 

/// <summary>

/// Gets or sets the value.

/// </summary>

/// <value>The value.</value>

[ConfigurationProperty("value", IsRequired = true)]

public string Value

{

get { return this["value"] as string; }

set { this["value"] = value as string; }

}

}

 

Second we need to create the custom ConfigurationSection :

public class MyCustomSection : ConfigurationSection

{

/// <summary>

/// Gets or sets the property 1.

/// </summary>

/// <value>The property 1.</value>

[ConfigurationProperty("Property1", IsRequired = true)]

public MyConfigurationElement Property1

{

get { return (MyConfigurationElement)this["Property1"]; }

set { this["Property1"] = value; }

}

 

/// <summary>

/// Gets or sets the Property 2.

/// </summary>

/// <value>The Property 2.</value>

[ConfigurationProperty("Property2", IsRequired = true)]

public MyConfigurationElement Property2

{

get { return (MyConfigurationElement)this["Property2"]; }

set { this["Property2"] = value; }

}

 

}

 

This custom section has 2 MyConfigurationElements: Property1 and Property2

And finally, the application config

<sectionGroup name="CustomGroup" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >

<section name="CustomSection" type=" MyCustomSection, Version=1.0.0.0, Culture=neutral " />

</sectionGroup>

 

<CustomGroup>

<CustomSection>

<Property1 value="0123456789" />

<Property2 value="Lorem ipsum dolor sit amet" />

</CustomSection>

</CustomGroup>

 

See here how you can add this section programmatically to your web.config

LEAVE A REPLY

Please enter your comment!
Please enter your name here