During development, I believe that everyone will often deal with configuration files, whether it is the App. config file of the application or the Web. config file of the Web program. Today, I have nothing to worry about. I suddenly thought that I was using the *. config file for configuration parameters, but I have never developed a custom configuration node. I did a little research and wrote this article to record my learning experience.
The configuration files involved in this article are App. config and Collector. config.
App. config
Collector. config
The configuration node of Collector. config is separated from App. config to demonstrate the use of configSource. ConfigSource is only available in custom section nodes.
The development of custom configuration nodes mainly involves the following types:
CollectorSectionGroup inherits the nodes corresponding to the ConfigurationSectionGroup and is mainly used to define a set of custom nodes. The Code is as follows:
[ConfigurationProperty(, IsRequired = { Sections[] }
The BackupSection inherits from the nodes corresponding to the ConfigurationSection and is mainly used to define custom nodes. Class defines a Path attribute and a TriggedTimes element set attribute, corresponding to the node is and, respectively, the Code is as follows:
[ConfigurationProperty(, IsRequired = { [ { [] = [ConfigurationProperty(, IsRequired = [ConfigurationCollection( { [] { [] = }
The TriggedTimeCollection inherits from the node corresponding to ConfigurationElementCollection. The Code is as follows:
}
TriggedTime is inherited from the node corresponding to the ConfigurationElement. The Code is as follows:
[ConfigurationProperty(, IsRequired = { (TimeSpan)[ { [] = }
The Demo code mainly demonstrates the display
Main( configuration = sectionGroup = configuration.GetSectionGroup() (sectionGroup != sectionGroup.BackupSection.Path = configuration = sectionGroup = (CollectorSectionGroup)configuration.GetSectionGroup( (sectionGroup != (TriggedTime triggedTime Console.WriteLine( }
If you need to modify the Web. config configuration information of a Web application, unlike App. cnfig, you need to use the WebConfigurationManager class instead of the ConfigurationManager class. The Code is as follows:
configuration = WebConfigurationManager.OpenWebConfiguration( settings = settings.Remove( settings.Add( configuration.Save();
Click here to download the Code involved in this article.