獲取App.config配置文件中的各個(gè)參數(shù)值
來(lái)源:易賢網(wǎng) 閱讀:1390 次 日期:2015-08-13 15:49:31
溫馨提示:易賢網(wǎng)小編為您整理了“獲取App.config配置文件中的各個(gè)參數(shù)值”,方便廣大網(wǎng)友查閱!

這篇文章介紹了獲取app.config配置文件中的參數(shù)值方法,首先是要添加System.Configuration引用,其次類(lèi)文件中必須有 using System.Configuration;再次App.config添加,最后向App.config配置文件添加參數(shù),下面通過(guò)列子給大家講解下,需要的朋友可以參考下

下面通過(guò)代碼示例給大家展示下,具體內(nèi)容如下:

首先添加System.Configuration引用

向App.config配置文件添加參數(shù)

App.config添加

向App.config配置文件添加參數(shù)

例子:

在這個(gè)App.config配置文件中,我添加了4個(gè)參數(shù),App.config參數(shù)類(lèi)似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>

那如何訪問(wèn)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();

}

通過(guò)Key訪問(wèn)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ù)值的例子,有需要的朋友可以參考下。

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:獲取App.config配置文件中的各個(gè)參數(shù)值
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)