web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Using Custom Configuration ...

Using Custom Configuration Section and Configuration Manager class in .NET – 1

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee
At times instead of using appSettings section we would like to define our own custom section within configuration file.
 
Suppose we  want to create a custom config section in this following manner
 
<SimplConfigSection id=myID name=myName />
 
For implementing the above custom section we need to first define a class inheriting from ConfigurationSection class
 
Add reference to System.Configuration dll.
 
Create a new config section class in the following manner
 
// inherit the class ConfigurationSection
    class SimpleConfigSection : ConfigurationSection
    {
        public SimpleConfigSection()
        {
        }
 
        // using ConfigurationProperty attribute to define the attribute
        [ConfigurationProperty(“id”,IsRequired = true)]
        public String ID
        {
            get
            { return (String)this[“id”]; }
            set
            { this[“id”] = value; }
        }
        // using ConfigurationProperty attribute to define the attribute
        [ConfigurationProperty(“name”, IsRequired = true)]
        public String Name
        {
            get
            { return (String)this[“name”]; }
            set
            { this[“name”] = value; }
        }
    }
 
 
Now to register your custom section do the following
 
<configSections>
<section name=SimplConfigSection            type=Namespace.SimpleConfigSection, Namespace />
</configSections>
 
 
And to use it within the application
 
SimpleConfigSection simpleConfig = (SimpleConfigSection)ConfigurationManager.GetSection(“SimplConfigSection”);
            string id = simpleConfig.ID;
            string name = simpleConfig.Name;
 
 
That’s it …

Posted in .NET Framework Tagged: .NET Framework

This was originally posted here.

Comments

*This post is locked for comments