应用C#进行利用开发时,有时须要在程序配置文件中增加自定义的配置项,此时能够应用ConfigurationSection。
本文参考链接:ConfigurationSection、ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty及James Johnson的回复。文中有误的中央欢送大家交换斧正。
ConfigurationProperty示意配置元素的一个个性或子级。能够设置其名称、类型和默认值 ,并能够指定该属性是否是必须的,是否为Key等。
ConfigurationElement示意配置文件中的配置元素,每一条配置项对应一条ConfigurationElement。ConfigurationElement是抽象类,通过继承该类能够增加自定义配置属性。
ConfigurationElementCollection示意配置文件中元素的汇合,蕴含一组配置项。ConfigurationElementCollection继承ConfigurationElement,并实现ICollection接口。
ConfigurationSection示意配置文件中的节。通过继承该类能够不便的扩大自定义的节点信息,通过ConfigurationManager.GetSection()
办法能够获取到自定义的Section。
示例
// config配置文件<configSections> <section name="customSection" type="ConsoleApplication1.CustomConfigurationSection,ConsoleApplication1"/></configSections><customSection> <customElementsCollection> <customElement name="name1" content="content1"/> <customElement name="name2" content="content2"/> <customElement name="name3" content="content3"/> </customElementsCollection></customSection>// ConfigurationElementpublic class CustomConfigurationElement : ConfigurationElement{ public CustomConfigurationElement(string name, string content) { this.Name = name; this.Content = content; } public CustomConfigurationElement() { } [ConfigurationProperty("name", IsRequired = true, IsKey = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("content", IsRequired = true)] public string Content { get { return (string)this["content"]; } set { this["content"] = value; } }}// ConfigurationElementCollectionpublic class CustomConfigurationElementCollection : ConfigurationElementCollection{ public CustomConfigurationElementCollection() { } protected override ConfigurationElement CreateNewElement() { return new CustomConfigurationElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((CustomConfigurationElement)element).Name; } protected override bool IsElementName(string elementName) { bool isName = false; if(!string.IsNullOrEmpty(elementName)) isName = elementName.Equals("customElement"); return isName; } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } protected override string ElementName { get { return "customElement"; } } public CustomConfigurationElement this[object key] { get { return (CustomConfigurationElement)BaseGet(key); } } public CustomConfigurationElement this[int index] { get { return (CustomConfigurationElement)BaseGet(index); } set { if(BaseGet(index) != null) BaseRemoveAt(index); BaseAdd(index, value); } }}// ConfigurationSectionpublic class CustomConfigurationSection : ConfigurationSection{ public CustomConfigurationSection() { } [ConfigurationProperty("customElementsCollection", IsDefaultCollection = false)] public CustomConfigurationElementCollection CustomElementsCollection { get { return (CustomConfigurationElementCollection)base["customElementsCollection"]; } }}// usepublic static void ReadConfigurationSection(string sectionName){ try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration; var customSection = config.GetSection(sectionName) as CustomConfigurationSection; if(customSection == null) { Console.WriteLine($"read failed: {sectionName} is null"); return; } Console.WriteLine("custom configuration section info:"); for (int i = 0; i < customSection.CustomElementsCollection.Count; ++i) { Console.WriteLine($"\tName:{customSection.CustomElementsCollection[i].Name} Content:{customSection.CustomElementsCollection[i].Content}"); } } catch (ConfigurationErrorsException err) { Console.WriteLine($"read configuration error: {err.Message}"); }}