C# 获取config文件的值
自定义配置文件帮助类
利用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文件的值的更多相关文章
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- C# 获取或设置指定 config 文件的值
ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...
- 对获取config文件的appSettings节点简单封装
转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...
- 获取 config文件的节点值
System.Configuration.ConfigurationManager.AppSettings["followTemplate"];
- C# 获取config文件 实体转换
随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...
- Struts2学习:Action获取properties文件的值
配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...
- Spring3.x 获取properties资源文件的值
Spring3.x 获取properties资源文件的值有两种方式: 第一种:使用<context:property-placeholder />标签 <context:prop ...
- WinForm读取指定的config文件的内容
config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...
- Config文件的使用:通过程序修改Config文件
对于config文件,一般情况下都是使用ConfigurationManager加载,然后通过读取相应节点的值来获取想要的数据,但是,有时候需要修改config文件的值,这时候就用到了OpenExeC ...
随机推荐
- 手动处理TFS数据仓库服务和分析服务
当您需要报告中最新的数据时,当发生错误时,或者在解决了模式冲突之后,您可以手动处理Team Foundation Server(TFS)关系数据库(TFSHStor)或SQLServer Analys ...
- Team Foundation Server (TFS) 2017 团队资源管理器
在千呼万唤中,TFS 2017团队资源管理器终于发布了,对于所有TFS系统的用户,都是一个天大的喜讯,尤其是对于不经常做.NET开发的团队成员. 大家都知道,伴随TFS 2013(和之前的版本)的发布 ...
- linux系统编程之文件与IO(三):利用lseek()创建空洞文件
一.lseek()系统调用 功能说明: 通过指定相对于开始位置.当前位置或末尾位置的字节数来重定位 curp,这取决于 lseek() 函数中指定的位置 函数原型: #include <sys/ ...
- mysql之使用centos7实现主从复制(读写分离)的实现过程
什么是主从复制? 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库:主数据库一般是准实时的业务数据库. 主从复制的作用(好处)! 1.做数据的热备,作为后备数据库,主数据库服务器故 ...
- Win(Phone)10开发第(5)弹,本地媒体服务器的一些注意事项
首先有个wp上的http服务器 http://wphttpserver.codeplex.com/ 使用方式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- Day 7 深copy和浅Copy
dict.fromkeys的用法 1 2 3 4 5 6 7 8 9 10 11 #dict.fromkeys的用法 #例子1 dic = dict.fromkeys([1,2,3],[]) prin ...
- Tomcat 服务器开启失败故障
Server Tomcat v8.0 Server at localhost failed to start.故障 原因:1.可能是web.xml中的filter-mapping中url-patter ...
- Java之常用类库
1.Java常用类库(一):http://blog.csdn.net/e6894853/article/details/7925469 1.Java常用类库(二):http://blog.csdn.n ...
- JDK,常见数据结构解读
一.情有独钟 对数据结构情有独钟,打算慢慢把jdk里的实现都读一遍,发现其中的亮点,持续更新. 二.ArrayList 这应该是我们学习java最早接触的到的数据结构,众所周知,数组在申请了内存之后, ...
- 怎么在eclipse中访问webservice
在eclipse创建webservice的方法: 1.在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 P ...