.Net — ConfigurationSection的简单使用

  • A+
所属分类:.NET技术
摘要

使用C#进行应用开发时,有时需要在程序配置文件中添加自定义的配置项,此时可以使用ConfigurationSection。

使用C#进行应用开发时,有时需要在程序配置文件中添加自定义的配置项,此时可以使用ConfigurationSection。

本文参考链接:ConfigurationSectionConfigurationElementConfigurationElementCollectionConfigurationPropertyJames 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>  // ConfigurationElement public 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; }     } }  // ConfigurationElementCollection public 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);         }     } }  // ConfigurationSection public class CustomConfigurationSection : ConfigurationSection {     public CustomConfigurationSection()     {     }      [ConfigurationProperty("customElementsCollection", IsDefaultCollection = false)]     public CustomConfigurationElementCollection CustomElementsCollection     {         get { return (CustomConfigurationElementCollection)base["customElementsCollection"]; }     } }  // use public 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}");     } }