国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

.NET之如何獲取App.config配置文件中的參數(shù)值

2019-11-17 02:14:36
字體:
供稿:網(wǎng)友

.NET之如何獲取App.config配置文件中的參數(shù)值

  • 首先的添加System.Configuration引用
  • 類文件中必須有 using System.Configuration;
  • App.config添加
  • 向App.config配置文件添加參數(shù)

  例子:

  在這個App.config配置文件中,我添加了4個參數(shù),App.config參數(shù)類似HashTable都是鍵/值對

<?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="the

  那如何訪問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遍歷出后保存在一個容器里(例如:數(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并添加進theKeys集合                foreach (string theKey in ConfigurationManager.AppSettings.Keys)                {                    theKeys.Add(theKey);                }                //根據(jù)Key遍歷出所有的Value并添加進theValues集合                for (int i = 0; i < theKeys.Count; i++)                {                    foreach (string theValue in ConfigurationManager.AppSettings.GetValues(theKeys[i]))                    {                        theValues.Add(theValue);                    }                }                //驗證一下                Console.WriteLine("*************Key*************");                foreach (string s in theKeys)                {                    Console.WriteLine(s);                }                Console.WriteLine("************Value************");                foreach (var item in theValues)                {                    Console.WriteLine(item);                }            }            Console.ReadKey();        }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 德格县| 大新县| 公主岭市| 昭苏县| 阿鲁科尔沁旗| 平泉县| 磐石市| 普兰县| 武强县| 宜章县| 来安县| 鸡西市| 博兴县| 河津市| 东台市| 铁力市| 华池县| 金湖县| 宜兴市| 新龙县| 尼玛县| 和顺县| 江源县| 合肥市| 忻州市| 上高县| 金寨县| 离岛区| 青海省| 朝阳市| 塔城市| 木兰县| 迭部县| 章丘市| 谢通门县| 分宜县| 北安市| 馆陶县| 寿光市| 顺义区| 嘉黎县|