獲取App.config配置文件中的參數(shù)值
下面通過代碼示例給大家展示下,具體內(nèi)容如下:
首先添加System.Configuration引用
向App.config配置文件添加參數(shù)
App.config添加
向App.config配置文件添加參數(shù)
例子:
在這個(gè)App.config配置文件中,我添加了4個(gè)參數(shù),App.config參數(shù)類似HashTable都是鍵/值對(duì)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="theDate" value="2015-07-20 16:25"/> <add key="theName" value="Alice"/> <add key="theType" value="NBA"/> <add key="thePrice" value="12500.00"/> </appSettings> </configuration>
那如何訪問App.config配置文件中的參數(shù)值呢?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace AppConfigDemo { class Program { static void Main(string[] args) { //判斷App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //循環(huán)遍歷出配置文件中的所有的鍵Key foreach (string s in ConfigurationManager.AppSettings) { Console.WriteLine(s); } } Console.ReadKey(); } } }
使用for循環(huán)遍歷Key的代碼如下:
static void Main(string[] args) { //判斷App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //循環(huán)遍歷出配置文件中的所有的鍵Key for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++) { Console.WriteLine(ConfigurationManager.AppSettings.GetKey(i)); } } Console.ReadKey(); }
通過Key訪問Value的方法:
static void Main(string[] args) { //判斷App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { //獲取“theDate”鍵的Value foreach (string s in ConfigurationManager.AppSettings.GetValues("theDate")) { Console.WriteLine(s); } } Console.ReadKey(); }
如果你想獲取所有Key的Value集合,那該怎么辦呢?
思路:將所有的Key遍歷出后保存在一個(gè)容器里(例如:數(shù)組),然后用Key匹配找出Value即可。
static void Main(string[] args) { //判斷App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) { List<string> theKeys = new List<string>(); //保存Key的集合 List<string> theValues = new List<string>(); //保存Value的集合 //遍歷出所有的Key并添加進(jìn)theKeys集合 foreach (string theKey in ConfigurationManager.AppSettings.Keys) { theKeys.Add(theKey); } //根據(jù)Key遍歷出所有的Value并添加進(jìn)theValues集合 for (int i = 0; i < theKeys.Count; i++) { foreach (string theValue in ConfigurationManager.AppSettings.GetValues(theKeys[i])) { theValues.Add(theValue); } } //驗(yàn)證一下 Console.WriteLine("*************Key*************"); foreach (string s in theKeys) { Console.WriteLine(s); } Console.WriteLine("************Value************"); foreach (var item in theValues) { Console.WriteLine(item); } } Console.ReadKey(); }
以上代碼就是使用.net技術(shù)獲取app.config配置文件中的參數(shù)值的例子,有需要的朋友可以參考下。
相關(guān)文章
ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法,是asp.net操作office文件的一個(gè)典型應(yīng)用,代碼中備有較為詳盡的注釋便于讀者理解,需要的朋友可以參考下2014-11-11DataGrid使用心得(調(diào)用及連接數(shù)據(jù)庫等等)
在工作中遇到把DataGrid中綁定的后臺(tái)數(shù)據(jù)庫數(shù)據(jù)展示給用戶時(shí)把負(fù)數(shù)變?yōu)?的小問題,現(xiàn)在把它記錄下來包括DataGrid的調(diào)用/連接數(shù)據(jù)庫進(jìn)行操作等等,感興趣的朋友可以了解下,或許本新得對(duì)你有所幫助2013-02-02.NET使用Hisql實(shí)現(xiàn)菜單管理(增刪改查)
這篇文章介紹了.NET使用Hisql實(shí)現(xiàn)菜單管理(增刪改查)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07關(guān)閉子頁面刷新父頁面中部分控件數(shù)據(jù)的方法
關(guān)閉子頁面刷新父頁面中部分控件數(shù)據(jù),具體的實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-05-05the sourcesafe database has been locked by the administrator
今天早上打開soucesafe的時(shí)候出現(xiàn)提示:“the sourcesafe database has been locked by the administrator"。仔細(xì)想想, 可能是前天晚上用"f:\analyze.exe" -I- -DB -F -V3 -D "f:\vssData\data" 命今分析的時(shí)候鎖定了database2009-04-04