以下非原创作品,但都是自己看过理解并写过,记录下来,以便之后项目的使用或其它用途。
(1)只需要简单配置单一属性值:
1 <configuration>View Code
2 <configSections>
3 <!--配置读取的全名称-->
4 <section name="simple" type="ConfigNode.SimpleSection,ConfigNode"/>
5 </configSections>
6 <system.web>
7 <compilation debug="true" targetFramework="4.0" />
8 </system.web>
9 <!--自定义单一数据-->
10 <simple maxValue="20" minValue="1"></simple>
11 </configuration>
要获取在配置文件中自定义的值,此时在我们上面配置的ConfigNode.SimpleSection,ConfigNode,根据这个创建SimpleSection类,在其中写上获取此节点的属性
1 public class SimpleSection : ConfigurationSection//必须要继承这个类View Code
2 {
3 /// <summary>
4 /// 实例化配置属性 元素是否必须 默认值
5 /// </summary>
6 [ConfigurationProperty("maxValue", IsRequired = false, DefaultValue = Int32.MaxValue)]
7 public int MaxValue
8 {
9 get
10 {
11 //配置文件中的节
12 return (int)base["maxValue"];
13 }
14 set
15 {
16 base["maxValue"] = value;
17 }
18 }
19 [ConfigurationProperty("minValue", IsRequired = false, DefaultValue = 1)]
20 public int MinValue
21 {
22 get { return (int)base["minValue"]; }
23 set { base["minValue"] = value; }
24 }
25 }
使用:
1 SimpleSection simple = ConfigurationManager.GetSection("simple") as SimpleSection;View Code
2 int maxValue = simple.MaxValue;
3 int minValue = simple.MinValue;
(2)在配置节点的头部,我们也需要配置一个属性值的话
1 <configSections>View Code
2 <section name="colors" type="ConfigNode.ColorsSection,ConfigNode" />
3 </configSections>
4 <colors type="颜色">
5 <color id="skyblue" name="天蓝色"/>
6 </colors>
ColorsSection类:
1 public class ColorsSection : ConfigurationSectionView Code
2 {
3 [ConfigurationProperty("type", IsRequired = true)]
4 public string Type
5 {
6 get
7 {
8 return (string)base["type"];
9 }
10 set
11 {
12 base["type"] = value;
13 }
14 }
15 [ConfigurationProperty("color", IsDefaultCollection = false)]
16 public ColorSection Color
17 {
18 get
19 {
20 return (ColorSection)base["color"];
21 }
22 set
23 {
24 base["color"] = value;
25 }
26 }
27
28 }
29
30 public class ColorSection : ConfigurationElement
31 {
32 [ConfigurationProperty("id", IsRequired = true, IsKey = true)]
33 public string Id
34 {
35 get
36 {
37 return (string)base["id"];
38 }
39 set
40 {
41 base["id"] = value;
42 }
43 }
44 [ConfigurationProperty("name", IsRequired = true)]
45 public string Name
46 {
47 get
48 {
49 return (string)base["name"];
50 }
51 set
52 {
53 base["name"] = value;
54 }
55 }
56 }
使用和第一的相同
(3)配置多个节点:
1 configuration>View Code
2 <configSections>
3 <!--这里name的名字 必须与创建的类的名字相同-->
4 <section name="AnimalSection" requirePermission="false" type="ConfigNode.AnimalSection,ConfigNode"/>
5 </configSections>
6
7 <system.web>
8 <compilation debug="true" targetFramework="4.0" />
9 </system.web>
10 <!--这里的也是-->
11 <AnimalSection>
12 <add cname="小狗" ename="dog" />
13 <add cname="小猫" ename="cat" />
14 <add cname="小兔" ename="rabbit" />
15 </AnimalSection>
AnimalSection类:
1 // 所有配置节点都要选择这个基类 ConfigurationSectionView Code
2 public class AnimalSection : ConfigurationSection
3 {
4
5
6 private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(AnimalCollect), null, ConfigurationPropertyOptions.IsDefaultCollection);
7 [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
8 public AnimalCollect ParamCollection
9 {
10 get
11 {
12 return (AnimalCollect)base[s_property];
13 }
14 }
15
16
17 }
18 /// <summary>
19 /// 自定义一个集合
20 /// </summary>
21 [ConfigurationCollection(typeof(Animal))]
22 public class AnimalCollect : ConfigurationElementCollection
23 {
24 // 基本上,所有的方法都只要简单地调用基类的实现就可以了。
25 public AnimalCollect()
26 : base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
27 {
28
29 }
30 // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
31 new public Animal this[string cname]
32 {
33 get
34 {
35 return (Animal)base.BaseGet(cname);
36 }
37 }
38 // 下面二个方法中抽象类中必须要实现的。
39 protected override ConfigurationElement CreateNewElement()
40 {
41 return new Animal();
42 }
43
44 protected override object GetElementKey(ConfigurationElement element)
45 {
46 return ((Animal)element).CName;
47 }
48 }
49
50
51 /// <summary>
52 /// 集合中的每个元素
53 /// </summary>
54 public class Animal : ConfigurationElement
55 {
56 [ConfigurationProperty("cname", IsRequired = true)]
57 public string CName
58 {
59 get
60 {
61 return this["cname"].ToString();
62 }
63 set
64 {
65 this["cname"] = value;
66 }
67 }
68
69 [ConfigurationProperty("ename", IsRequired = true)]
70 public string EName
71 {
72 get
73 {
74 return this["ename"].ToString();
75 }
76 set
77 {
78 this["ename"] = value;
79 }
80 }
81 }
使用:
1 var custSection = ConfigurationManager.GetSection("AnimalSection") as AnimalSection;View Code
2 var s = (from kv in custSection.ParamCollection.Cast<Animal>() select kv).ToList();
3 string str = string.Empty;
4 foreach (Animal item in s)
5 {
6 str += "中文名:" + item.CName + ",英文名:" + item.EName;
7 }
(4)配置节点下的多集合节点(键值类型)
1 <configuration>View Code
2 <configSections>
3 <!--这里name的名字 必须与创建的类的名字相同-->
4 <section name="AnimalSection" requirePermission="false" type="ConfigNode.AnimalSection,ConfigNode"/>
5 </configSections>
6
7 <system.web>
8 <compilation debug="true" targetFramework="4.0" />
9 </system.web>
10 <!--这里的也是-->
11 <AnimalSection>
12 <fly>
13 <add name="燕子" value="swallow" />
14 <add name="天鹅" value="swan" />
15 </fly>
16 <fish>
17 <add name="鲨鱼" value="shark"/>
18 <add name="金鱼" value="goldfish"/>
19 </fish>
20 <mammalia>
21 <add name="小狗" value="dog" />
22 <add name="小猫" value="cat" />
23 <add name="小兔" value="rabbit" />
24 </mammalia>
25 </AnimalSection>
26 </configuration>
AnimalSection类:
1 // 所有配置节点都要选择这个基类 ConfigurationSectionView Code
2 public class AnimalSection : ConfigurationSection
3 {
4 [ConfigurationProperty("mammalia", IsDefaultCollection = false)]
5 public NameValueConfigurationCollection Mammalia
6 {
7 get
8 {
9 return (NameValueConfigurationCollection)base["mammalia"];
10 }
11 set
12 {
13 base["mammalia"] = value;
14 }
15 }
16
17 [ConfigurationProperty("fly", IsDefaultCollection = false)]
18 public NameValueConfigurationCollection Fly
19 {
20 get
21 {
22 return (NameValueConfigurationCollection)base["fly"];
23 }
24 set
25 {
26 base["fly"] = value;
27 }
28 }
29
30 [ConfigurationProperty("fish", IsDefaultCollection = false)]
31 public NameValueConfigurationCollection Fish
32 {
33 get
34 {
35 return (NameValueConfigurationCollection)base["fish"];
36 }
37 set
38 {
39 base["fish"] = value;
40 }
41 }
42
43
44 }
使用:
1 AnimalSection animal = ConfigurationManager.GetSection("AnimalSection") as AnimalSection;View Code
2 string str = string.Empty;
3 foreach (string key in animal.Mammalia.AllKeys)
4 {
5 str += "中文名:" + key + ",英文名:" + animal.Mammalia[key].Value;
6 }
(5)配置节点下的多集合节点(自定义类型)
1 <configuration>View Code
2 <configSections>
3 <!--这里name的名字 必须与创建的类的名字相同-->
4 <section name="FamilySection" requirePermission="false" type="ConfigNode.FamilySection,ConfigNode"/>
5 </configSections>
6
7 <system.web>
8 <compilation debug="true" targetFramework="4.0" />
9 </system.web>
10 <FamilySection number="12">
11 <myself name="Z" age="1" sex="男" />
12 <familyMember>
13 <add name="ZR" age="2" sex="男" relation="父子" />
14 <add name="WY" age="2" sex="女" relation="母子" />
15 <add name="ZZ" age="1" sex="男" relation="兄弟" />
16 <add name="ZG" age="1" sex="女" relation="兄妹" />
17 <remove name="ZG" />
18 </familyMember>
19 </FamilySection>
20 </configuration>
FamilySection类:
1 using System.Configuration;View Code
2 public class FamilySection : ConfigurationSection
3 {
4 /// <summary>
5 /// 获取父节点自定义配置的值
6 /// </summary>
7 [ConfigurationProperty("number", IsRequired = true)]
8 public int Number
9 {
10 get
11 {
12 return (int)base["number"];
13 }
14 set
15 {
16 base["number"] = value;
17 }
18 }
19 [ConfigurationProperty("myself", IsDefaultCollection = false)]
20 public MySelfSection MySelf
21 {
22 get
23 {
24 return (MySelfSection)base["myself"];
25 }
26 set
27 {
28 base["myself"] = value;
29 }
30 }
31
32
33 [ConfigurationProperty("familyMember", IsRequired = false)]
34 [ConfigurationCollection(typeof(FamilyMemberSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
35 public FamilyMember FamilyMember
36 {
37 get
38 {
39 return (FamilyMember)base["familyMember"];
40 }
41 set
42 {
43 base["familyMember"] = value;
44 }
45 }
46 }
47
48
49 public class MySelfSection : ConfigurationElement
50 {
51 [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
52 public string Name
53 {
54 get { return (string)base["name"]; }
55 set { base["name"] = value; }
56 }
57 [ConfigurationProperty("age", IsRequired = true)]
58 public int Age
59 {
60 get { return (int)base["age"]; }
61 set { base["age"] = value; }
62 }
63 [ConfigurationProperty("sex", IsRequired = true)]
64 public string Sex
65 {
66 get { return (string)base["sex"]; }
67 set { base["sex"] = value; }
68 }
69 }
70 public class FamilyMemberSection : MySelfSection
71 {
72 [ConfigurationProperty("relation", IsRequired = true)]
73 public string Relation
74 {
75 get { return (string)base["relation"]; }
76 set { base["relation"] = value; }
77 }
78 }
79
80 public class FamilyMember : ConfigurationElementCollection
81 {
82
83
84 protected override ConfigurationElement CreateNewElement()
85 {
86 return new FamilyMemberSection();
87 }
88
89 protected override object GetElementKey(ConfigurationElement element)
90 {
91 return ((FamilyMemberSection)element).Name;
92 }
93
94 public FamilyMemberSection this[int i]
95 {
96 get
97 {
98 return (FamilyMemberSection)base.BaseGet(i);
99 }
100 }
101
102 public FamilyMemberSection this[string key]
103 {
104 get
105 {
106 return (FamilyMemberSection)base.BaseGet(key);
107 }
108 }
109 }
使用:
1 FamilySection family = ConfigurationManager.GetSection("FamilySection") as FamilySection;View Code
2 string number = family.Number.ToString();
3 string myself = family.MySelf.Name + "-" + family.MySelf.Age + "-" + family.MySelf.Sex;
4 string str = string.Empty;
5 foreach (FamilyMemberSection item in family.FamilyMember)
6 {
7 str += item.Name + "-" + item.Age + "-" + item.Sex + "-" + item.Relation;
8 }
(6)对配置多个section分组
1 <configuration>View Code
2 <configSections>
3 <!--这里name的名字 必须与创建的类的名字相同-->
4
5 <sectionGroup type="ConfigNode.TestSectionGroup,ConfigNode" name="textgroup">
6 <section name="score" type="ConfigNode.ScoreSection,ConfigNode" allowDefinition="Everywhere"/>
7 <section name="project" type="ConfigNode.ProjectSection,ConfigNode" allowDefinition="Everywhere"/>
8 </sectionGroup>
9 </configSections>
10
11 <system.web>
12 <compilation debug="true" targetFramework="4.0" />
13 </system.web>
14 <textgroup>
15 <score chinese="20"></score>
16 <project name="测试"></project>
17 </textgroup>
需要单独配置Group的类:
1 public class TestSectionGroup : ConfigurationSectionGroupView Code
2 {
3 public ProjectSection Project
4 {
5 get
6 {
7 return (ProjectSection)base.Sections["project"];
8 }
9
10 }
11
12 public ScoreSection Score
13 {
14 get
15 {
16 return (ScoreSection)base.Sections["score"];
17 }
18 }
19 }
使用:
1 //在exe中使用 TestSectionGroup group = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["textgroup"];View Code
2 //在web程序中使用:续引用System.Web.Configuration;
3 TestSectionGroup group = (TestSectionGroup)WebConfigurationManager.OpenWebConfiguration("~").SectionGroups["textgroup"];
4
5 string name = group.Project.Name;