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

来源:http://www.cnblogs.com/wuchen1314/archive/2016/02/19/5197984.html
-Advertisement-
Play Games

以下非原創作品,但都是自己看過理解並寫過,記錄下來,以便之後項目的使用或其它用途。 (1)只需要簡單配置單一屬性值: 1 <configuration> 2 <configSections> 3 <!--配置讀取的全名稱--> 4 <section name="simple" type="Confi


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

(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         {

              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這個是2011年寫的一個技術貼,現在看可能有點過時了,有興趣的朋友也可以看一下。 一. 描述 在開發winform程式時不會考慮頁面刷新問題,只要控制好線程別導致假死就ok了,但是在開發web頁面程式時,刷新有的時候真是頭痛的很。頁面回調可以解決這個問題,下麵簡單就實用來講對callback做下介紹
  • 前言: NPOI 技術在別人看來可能有點老生長談了,但是對於我現在處於的這個創業型公司,還是挺前沿的,不知道是前輩們不知道這個技術,還是懶得去對現有的軟體進行修改,因為在現有的軟體中,幾乎所有的數據導入導出都是依賴於: Excel.Application(設置excel組件,系統許可權,然後還得考慮版
  • 1、sealed 修飾符 概念: C#提出了一個密封類(sealed class)的概念,幫助開發人員來解決這一問題。 密封類在聲明中使用sealed 修飾符,這樣就可以防止該類被其它類繼承。如果試圖將一個密封類作為其它類的基類,C#將提示出錯。理所當然,密封類不能同時又是抽象類,因為抽象總是希望被
  • ---恢復內容開始--- RazorEngine模板引擎大大的幫助了我們簡化字元串的拼接與方法的調用,開源之後,現在在簡單的web程式,winform程式,甚至控制台程式都可以利用它來完成。 但如何在使用中調用方法和使用自定義模板呢?來看這樣一個例子 1 string str="hello @Mod
  • 使用Visual Studio寫代碼,經常遇到的一個問題就是切換中文輸入法麻煩,輸入完註釋//,要切換到中文,輸入完引號,要輸入中文,然後還需要切換回來,有沒有? 有時候中文輸入法忽然失效有沒有?明明在中文輸入法狀態下,輸入不了中文,有沒有?
  • 話說有一天,臨近下班無心工作,在網上看各種文章,閱讀到了一篇名為《聊聊大麥網UWP版的首頁頂部圖片聯動效果的實現方法》(傳遞:http://www.cnblogs.com/hippieZhou/p/4755290.html),看到別人評論自己做的產品,頓時來了興趣,閱讀過後,hippieZhou童鞋
  • 近段時間,需要寫一個小功能,就是需要判斷程式是否已經運行。某個程式安裝後,也許被多個用戶運行。那怎樣判斷當前用戶已經運行了此程式了呢?下麵是Insus.NET的做法,就是:《VB.NET WinForm獲取運行程式用戶名》http://www.cnblogs.com/insus/p/5194839.
  • (轉)這裡給大家分享幾個VS版本,都是最終版的,也是中文版的!. Visual Studio 2005:http://pan.baidu.com/s/1c0eudyS Visual Studio 2008:http://pan.baidu.com/s/1i3GJ7pj Visual Studio 2
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...