1. 程式人生 > >C#配置文件Section節點處理總結

C#配置文件Section節點處理總結

mod ret pro one void apps 實時 title als

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

很多時候在項目開發中,我們都需要用配置文件來存儲一些關於程序配置信息,這時候你可以選擇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;

}

}

}

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#配置文件Section節點處理總結

本文地址: http://www.paobuke.com/develop/c-develop/pbk23437.html






相關內容

技術分享圖片C#中for循環、while循環循環執行的方法技術分享圖片C#圖片處理3種高級應用技術分享圖片C#中Socket通信用法實例詳解技術分享圖片C#實現將數據導出到word或者Excel中的方法
技術分享圖片C#中使用DataContractSerializer類實現深拷貝操作示例技術分享圖片C# WinForm程序處理後臺繁忙導致前臺控件假死現象解決方法技術分享圖片C#中ref和out的區別淺析技術分享圖片C#引用類型作為方法的參數分析

C#配置文件Section節點處理總結