Custom application configuration node

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

During development, you often have to deal with and the configuration file, regardless of the application's App.config file, or a Web program Web.config file. Nothing to do today, suddenly thought of just the *.config files for configuration parameters, but never did develop custom configuration node, slightly a little research, writing this article record the learning experience.

First introduce the configuration file to look in this paper are App.config and Collector.config.

App.config

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <configSections>
 4     <sectionGroup name="collector" type="Labs.CollectorSectionGroup, Labs">
 5       <section name="backup" type="Labs.BackupSection, Labs"/>
 6     </sectionGroup>
 7   </configSections>
 8   <startup>
 9     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
10   </startup>
11   <collector>
12     <backup configSource="Collector.config" />
13   </collector>
14 </configuration>

Collector.config

1 <?xml version="1.0" encoding="utf-8" ?>
2 <backup path="D:\Downloads">
3   <triggedTimes>
4     <add time="10:00:00" />
5     <add time="12:00:00" />
6   </triggedTimes>
7 </backup>

Separate configuration node Collector.config from App.config, mainly in order to demonstrate the use of configSource. ConfigSource only in the custom section  nodes available;.

The development of custom configuration node mainly involves the following several kinds:

CollectorSectionGroup inherits the node from the ConfigurationSectionGroup corresponding to   <sectionGroup name= "collector" type= "Labs.CollectorSectionGroup, Labs" > and <collector>, is mainly used to define a custom set of nodes, the code shown below:

 1 using System.Configuration;
 2 
 3 namespace Labs
 4 {
 5     public sealed class CollectorSectionGroup : ConfigurationSectionGroup
 6     {
 7         [ConfigurationProperty("backup", IsRequired = true)]
 8         public BackupSection BackupSection
 9         {
10             get { return Sections["backup"] as BackupSection; }
11         }
12     }
13 }

BackupSection inherits the node from the ConfigurationSection corresponding to <section name= "backup" type= "Labs.BackupSection, Labs" /> and <backup configSource= "Collector.config" />, is mainly used to define a custom node;. Class defines a Path property and the TriggedTimes element set attribute, respectively corresponding to node <backup path= "D:\Downloads" > and <triggedTimes>, the code shown below:

 1 using System.Configuration;
 2 
 3 namespace Labs
 4 {
 5     public sealed class BackupSection : ConfigurationSection
 6     {
 7         [ConfigurationProperty("path", IsRequired = true)]
 8         public string Path
 9         {
10             get { return this["path"].ToString(); }
11             internal set { this["path"] = value; }
12         }
13 
14         [ConfigurationProperty("triggedTimes", IsRequired = true)]
15         [ConfigurationCollection(typeof(TriggedTime))]
16         public TriggedTimeCollection TriggedTimes
17         {
18             get { return this["triggedTimes"] as TriggedTimeCollection; }
19             internal set { this["triggedTimes"] = value; }
20         }
21     }
22 }

 TriggedTimeCollection ConfigurationElementCollection inherits the node from the corresponding <triggedTimes>, the code shown below:

 1 using System.Configuration;
 2 
 3 namespace Labs
 4 {
 5     public sealed class TriggedTimeCollection : ConfigurationElementCollection
 6     {
 7         protected override ConfigurationElement CreateNewElement()
 8         {
 9             return new TriggedTime();
10         }
11 
12         protected override object GetElementKey(ConfigurationElement element)
13         {
14             return element.GetHashCode();
15         }
16     }
17 }

 TriggedTime Inherited from the ConfigurationElement  the corresponding node for the <add time= "10:00:00" />, the code shown below;:

 1 using System;
 2 using System.Configuration;
 3 
 4 namespace Labs
 5 {
 6     public sealed class TriggedTime : ConfigurationElement
 7     {
 8         [ConfigurationProperty("time", IsRequired = true)]
 9         public TimeSpan Time
10         {
11             get { return (TimeSpan)this["time"]; }
12             internal set { this["time"] = value; }
13         }
14     }
15 }

Demo code mainly demonstrates that <collector> node configuration information and update the <backup path= "D:\Downloads" > in the path value, the code shown below:

 1 using System;
 2 using System.Configuration;
 3 
 4 namespace Labs
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             DisplayConfigurations();
11             var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
12             var sectionGroup = configuration.GetSectionGroup("collector") as CollectorSectionGroup;
13             if (sectionGroup != null)
14             {
15                 sectionGroup.BackupSection.Path = @"D:\Test";
16             }
17             configuration.Save(ConfigurationSaveMode.Full);
18             DisplayConfigurations();
19         }
20 
21         /// <summary>
22         /// Display configuration information. 
23         /// </summary>
24         static void DisplayConfigurations()
25         {
26             var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
27             var sectionGroup = (CollectorSectionGroup)configuration.GetSectionGroup("collector");
28 
29             if (sectionGroup != null) {
30                 Console.WriteLine(sectionGroup.BackupSection.Path);
31                 foreach (TriggedTime triggedTime in sectionGroup.BackupSection.TriggedTimes)
32                 {
33                     Console.WriteLine("Trigged time: {0}", triggedTime.Time);
34                 }
35             }
36         }
37     }
38 }

If the need for the Web application Web.config  configuration information changes, and the App.cnfig is different, need to use the WebConfigurationManager class instead of the ConfigurationManager class, the code shown below:

1                 var configuration = WebConfigurationManager.OpenWebConfiguration("~");
2                 var settings = configuration.AppSettings.Settings;
3                 settings.Remove("title");
4                 settings.Add("title", model.Title);
5                 configuration.Save();

This paper relates to the code can click here to download.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Adalheid at November 16, 2013 - 4:14 PM