码迷,mamicode.com
首页 > Windows程序 > 详细

C#配置文件Section节点处理总结

时间:2017-11-29 14:38:39      阅读:1195      评论:0      收藏:0      [点我收藏+]

标签: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>

<!–谷歌地图–>

<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#配置文件Section节点处理总结

标签:display   cursor   enter   数据导出   执行   ons   using   encoding   normal   

原文地址:http://www.cnblogs.com/paobuke/p/7920123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!