App.config中的自定义配置部分C#

我是c#中配置部分的初学者
我想在配置文件中创建自定义部分。 我在google搜索后尝试的内容如下
配置文件:

    

CustomSection.cs

  namespace CustomSectionTest { public class CustomSection : ConfigurationSection { [ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)] [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public String Key { get { return this["key"].ToString(); } set { this["key"] = value; } } } } 

当我使用此代码检索Section时,我收到一条错误说配置错误。

 var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection"); 

我错过了什么?
谢谢。

编辑
我最终需要的是

                

App.config中:

     

用法:

 NameValueCollection settings = ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings") as System.Collections.Specialized.NameValueCollection; if (settings != null) { foreach (string key in settings.AllKeys) { Response.Write(key + ": " + settings[key] + "
"); } }

尝试使用:

 var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MyCustomSections/CustomSection"); 

您需要节组的名称和自定义节。

突出显示ConfigurationSection按F1,您将看到MSDN网站上的实现覆盖了一个名为“Properties”的属性,该属性返回“ConfigurationPropertyCollection”,因为您的属性具有该类型的匹配属性,您应该能够使用您的属性填充此集合如果没有以MS家伙的方式包装它们。