最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#配置文件Section節(jié)點處理總結

 更新時間:2014年10月12日 17:07:33   投稿:shichen2014  
這篇文章主要介紹了C#配置文件Section節(jié)點處理總結,針對配置文件Section節(jié)點的處理做了較為詳細的實例總結,需要的朋友可以參考下

本文實例總結了C#配置文件Section節(jié)點處理方法。分享給大家供大家參考。具體如下:

很多時候在項目開發(fā)中,我們都需要用配置文件來存儲一些關于程序配置信息,這時候你可以選擇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"/>
      <!--箱實時狀態(tài)信息匯總-->
      <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>
        ///構造函數(shù)
        /// </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>
        /// 根據(jù)鍵獲取值
        /// </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;
        }
    }
}

測試效果如下:

希望本文所述對大家的C#程序設計有所幫助

相關文章

最新評論

佛教| 五指山市| 延川县| 威海市| 汝南县| 南京市| 潮安县| 关岭| 湘西| 长寿区| 白河县| 平利县| 合江县| 遂宁市| 福州市| 兰州市| 宿州市| 太康县| 文成县| 遵义市| 珲春市| 淮南市| 托克逊县| 柳河县| 临澧县| 邛崃市| 南京市| 张家界市| 清流县| 张家口市| 龙南县| 拉萨市| 阿荣旗| 扎赉特旗| 巢湖市| 平邑县| 拜城县| 金堂县| 巨鹿县| 上饶县| 大姚县|