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

首頁 > 學院 > 開發設計 > 正文

[C#]配置文件Section節點處理小計

2019-11-14 16:29:24
字體:
來源:轉載
供稿:網友

有時候項目開發中,需用用配置文件來存儲一些關于程序配置信息,這時候你可以選擇INI或者app.config來存儲,這里總結小計一下:

配置文件示例:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <sectionGroup name="module">      <section name="appSettings" type="System.Configuration.NameValueFileSectionHandler"/>    </sectionGroup>  </configSections>  <module>    <appSettings>      <!--谷歌地圖-->      <add key="Googlemap" value="1"/>      <!--箱實時狀態信息匯總-->      <add key="Cab_rt" value="1"/>    </appSettings>  </module></configuration>
操作代碼:
using System;using System.Collections.Specialized;using System.Configuration;namespace Consoleapplication38{    class PRogram    {        static void Main(string[] args)        {            try            {                SectionToolV2 _sectionHelper = new SectionToolV2("module/appSettings");                Console.WriteLine(_sectionHelper.GetValue("Googlemap"));                Console.WriteLine(_sectionHelper.ContainKey("YanZhiwei"));            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                Console.ReadLine();            }        }    }    class SectionToolV2    {        NameValueCollection ModulSettings = null;        /// <summary>        ///構造函數         /// </summary>        /// <param name="sectionName">section名稱</param>        public SectionToolV2(string sectionName)        {            ModulSettings = ConfigurationManager.GetSection(sectionName) as NameValueCollection;        }        /// <summary>        /// 是否包含該Section        /// </summary>        /// <returns></returns>        public bool ContainSection()        {            return !(ModulSettings == null);        }        /// <summary>        /// Section是否包含Key        /// </summary>        /// <param name="key">鍵</param>        /// <returns>值</returns>        public bool ContainKey(string key)        {            if (ContainSection())            {                return !(ModulSettings[key] == null);            }            return false;        }        /// <summary>        /// 根據鍵獲取值        /// </summary>        /// <param name="Key">鍵</param>        /// <returns>當不存在鍵的時候,返回string.Empty</returns>        public string GetValue(string Key)        {            string _value = string.Empty;            if (ContainKey(Key))            {                _value = ModulSettings[Key];            }            return _value;        }    }}

測試效果:

image

希望有所幫助!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西吉县| 万全县| 凌海市| 南江县| 铜川市| 茶陵县| 长丰县| 大丰市| 三原县| 宜春市| 炎陵县| 阳原县| 从化市| 五大连池市| 汶上县| 博乐市| 新闻| 和平区| 新建县| 昭苏县| 耿马| 安乡县| 盘锦市| 余江县| 阜康市| 汝州市| 长寿区| 蒲城县| 江城| 乐陵市| 福贡县| 北宁市| 秦安县| 阳谷县| 新丰县| 昌邑市| 罗源县| 九龙城区| 定州市| 九江县| 青浦区|