自定义配置文件帮助类

利用ExeConfigurationFileMap类将自定义配置文件转换为Configuration类进行数据读取

代码很简单,就不做扼要说明

  /// <summary>
/// 自定义配置文件
/// </summary>
public class ConfigHelper
{
#region 配置属性节点
private static string _test1;
/// <summary>
/// 获取Test1
/// </summary>
public static string Test1
{
get
{
if (string.IsNullOrWhiteSpace(_test1))
{
ConfigDataLoad();
}
return _test1;
}
} private static string _test2;
/// <summary>
/// 获取Test1
/// </summary>
public static string Test2
{
get
{
if (string.IsNullOrWhiteSpace(_test2))
{
ConfigDataLoad();
}
return _test2;
}
} private static string _test3;
/// <summary>
/// 获取Test1
/// </summary>
public static string Test3
{
get
{
if (string.IsNullOrWhiteSpace(_test3))
{
ConfigDataLoad();
}
return _test3;
}
}
#endregion #region 初始化配置 /// <summary>
/// 初始化配置文件
/// </summary>
private static void ConfigDataLoad()
{
//获取文件路径
string fileName = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\Test.config";
if (File.Exists(fileName))
{
ExeConfigurationFileMap file = new ExeConfigurationFileMap
{
ExeConfigFilename = fileName
};
//将文件转换为Configuration
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
SetData(config);
}
else
{
throw new Exception("配置文件不存在");
}
}
/// <summary>
/// 初始化值
/// </summary>
private static void SetData(Configuration config)
{
_test1 = config.AppSettings.Settings["test1"].Value;
_test2 = config.AppSettings.Settings["test2"].Value;
_test3 = config.AppSettings.Settings["test3"].Value;
}
#endregion }

C# 获取config文件的值的更多相关文章

  1. c# 根据配置文件路径,设置和获取config文件 appSettings 节点值

    /// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...

  2. C# 获取或设置指定 config 文件的值

    ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...

  3. 对获取config文件的appSettings节点简单封装

    转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...

  4. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  5. C# 获取config文件 实体转换

    随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...

  6. Struts2学习:Action获取properties文件的值

    配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...

  7. Spring3.x 获取properties资源文件的值

    Spring3.x 获取properties资源文件的值有两种方式:  第一种:使用<context:property-placeholder />标签  <context:prop ...

  8. WinForm读取指定的config文件的内容

    config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...

  9. Config文件的使用:通过程序修改Config文件

    对于config文件,一般情况下都是使用ConfigurationManager加载,然后通过读取相应节点的值来获取想要的数据,但是,有时候需要修改config文件的值,这时候就用到了OpenExeC ...

随机推荐

  1. C# 下载文件 删除文件 写入文本

    由于经常用到文件处理,便自己封装了下 分享给大家. 包含写入文本  批量删除文件  下载文件 .--可直接使用 /// <summary> /// 写入到txt /// </summ ...

  2. KEUC首次落地中国,网易云深度剖析Kubernetes优化与实践

    本文由  网易云发布. 10 月 15 日,聚焦 Kubernetes 中国行业应用与技术落地的首届中国 Kubernetes 用户大会(KEUC)在杭州成功举办.本次大会吸引了来自全球各地的技术精英 ...

  3. 程序媛计划——python爬虫

    #用selenium打开百度首页 #第一次运行代码时应该在safari开发者选项中设置'allow remote automation' from selenium import webdriver ...

  4. C++一些函数的意义

    1.重载 : C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,这就是函数的重载 2.隐藏和覆盖的区别 IF 子类的函数与父类的名称相同,但是参数不同 父类函数被隐藏(还存在) ELS ...

  5. A - Subsequence (算法 二分 )

    点击打开链接 A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 1 ...

  6. flask源码解析之session

    内容回顾 cookie与session的区别: 1. session 是保存在服务端的键值对 2. cookie 只能保存4096个字节的数据,但是session不受限制 3. cookie保存在浏览 ...

  7. 关于popup

    p1.html:点击添加按钮,开启窗口,打开p2.html,填写数据后返回p3.html,p3.html将数据回传到p1.html,且关闭自己   p1.html: <!DOCTYPE html ...

  8. asp.net mvc 5 初体验

    参考:http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 1. 新建 ASP.Net Web 应用程序,跟着向导一路 ...

  9. 使用browserSync自动刷新

    本篇主要是以 http://www.imooc.com/article/14759 为参考来写的: 已经整理到github上:https://github.com/Macaulish/gulp-Bro ...

  10. class字节码结构(一)(字节码结构和字节常量池的结构)

    <Java虚拟机原理图解> 1.1.class文件基本组织结构 关于变量的几个叫法: 局部变量/全局变量:很好区分根据所在位置. 类变量:静态的全局变量. 类常量:全局的final修饰的变 ...