C#設定檔Section節點處理總結

來源:互聯網
上載者:User

標籤:display   cursor   enter   資料匯出   執行   ons   using   encoding   normal   

本文執行個體總結了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>

<!–Google地圖–>

<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節點處理總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.