程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 自定義配置文件的使用(web.config/app.config),webconfig自定義配置

自定義配置文件的使用(web.config/app.config),webconfig自定義配置

編輯:C#入門知識

自定義配置文件的使用(web.config/app.config),webconfig自定義配置


以下非原創作品,但都是自己看過理解並寫過,記錄下來,以便之後項目的使用或其它用途。

(1)只需要簡單配置單一屬性值:

1 <configuration> 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> View Code

要獲取在配置文件中自定義的值,此時在我們上面配置的ConfigNode.SimpleSection,ConfigNode,根據這個創建SimpleSection類,在其中寫上獲取此節點的屬性

1 public class SimpleSection : ConfigurationSection//必須要繼承這個類 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 } View Code

使用:

1 SimpleSection simple = ConfigurationManager.GetSection("simple") as SimpleSection; 2 int maxValue = simple.MaxValue; 3 int minValue = simple.MinValue; View Code

 (2)在配置節點的頭部,我們也需要配置一個屬性值的話

1 <configSections> 2 <section name="colors" type="ConfigNode.ColorsSection,ConfigNode" /> 3 </configSections> 4 <colors type="顏色"> 5 <color id="skyblue" name="天藍色"/> 6 </colors> View Code

ColorsSection類:

1 public class ColorsSection : ConfigurationSection 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 } View Code

使用和第一的相同

(3)配置多個節點:

1 configuration> 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> View Code

AnimalSection類:

1 // 所有配置節點都要選擇這個基類 ConfigurationSection 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 } View Code

使用:

1 var custSection = ConfigurationManager.GetSection("AnimalSection") as AnimalSection; 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 } View Code

(4)配置節點下的多集合節點(鍵值類型)

1 <configuration> 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> View Code

AnimalSection類:

1 // 所有配置節點都要選擇這個基類 ConfigurationSection 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 } View Code

使用:

1 AnimalSection animal = ConfigurationManager.GetSection("AnimalSection") as AnimalSection; 2 string str = string.Empty; 3 foreach (string key in animal.Mammalia.AllKeys) 4 { 5 str += "中文名:" + key + ",英文名:" + animal.Mammalia[key].Value; 6 } View Code

(5)配置節點下的多集合節點(自定義類型)

1 <configuration> 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> View Code

FamilySection類:

1 using System.Configuration; 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 } View Code

使用:

1 FamilySection family = ConfigurationManager.GetSection("FamilySection") as FamilySection; 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 } View Code

(6)對配置多個section分組

1 <configuration> 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> View Code

需要單獨配置Group的類:

1 public class TestSectionGroup : ConfigurationSectionGroup 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 } View Code

使用:

1 //在exe中使用 TestSectionGroup group = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["textgroup"]; 2 //在web程序中使用:續引用System.Web.Configuration; 3 TestSectionGroup group = (TestSectionGroup)WebConfigurationManager.OpenWebConfiguration("~").SectionGroups["textgroup"]; 4 5 string name = group.Project.Name; View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved