对获取config文件的appSettings节点简单封装
转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html
C#的开发中,无论你是winform开发还是webform开发,都需要通过config文件来配置一些信息,因此我们也经常需要获取其中的 appSettings节点的值。当然,.net已经对访问这个节点做了封装,我们可以很方便的访问该节点。但是,我觉得还是不够满意,因为我需要在获取 不到节点的时候能够返回默认的值,获取的时候能够自动转为我需要的类型,我需要能够随时指定config文件,而不仅仅是默认的web.config文件 或者是*.exe.config文件。尤其最后的那个功能,嘿嘿,别以为这个没有用,有时候,我们是需要共用一个配置文件的。比如你在开发Windows 服务的时候,你不会希望你用界面的那个程序和Windows服务的程序有两个一样的配置文件吧
直接贴代码:
/// <summary>
/// 获取AppSetting里面的内容
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="defaultValue">The default value.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
public static T GetAppSettingValue<T>(T defaultValue, string key)
{
string value = ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value))
{
try
{
defaultValue = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
}
}
return defaultValue;
} /// <summary>
/// 获取指定的Config文件的AppSetting里面的指定键值对应的value值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="defaultValue">The default value.</param>
/// <param name="key">The key.</param>
/// <param name="file">The file.</param>
/// <returns></returns>
public static T GetAppSettingValue<T>(T defaultValue, string key, string file)
{
var map = new ExeConfigurationFileMap
{
ExeConfigFilename = file
};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
string value = config.AppSettings.Settings[key].Value; if (!string.IsNullOrEmpty(value))
{
try
{
defaultValue = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
}
}
return defaultValue;
}
对获取config文件的appSettings节点简单封装的更多相关文章
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- 获取 config文件的节点值
System.Configuration.ConfigurationManager.AppSettings["followTemplate"];
- C# 获取config文件的值
自定义配置文件帮助类 利用ExeConfigurationFileMap类将自定义配置文件转换为Configuration类进行数据读取 代码很简单,就不做扼要说明 /// <summary&g ...
- C# 获取config文件 实体转换
随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...
- 从Config文件中读取节点的配置信息
下面是web.config中与本内容有关的细节 <appSettings> <add key="servername" value="www" ...
- 关于config文件中AppSettings和ConnectionStrings的用法跟区别(转)
转自:http://www.cnblogs.com/bindot/archive/2013/03/07/def.html
- iOS - 音乐播放器需要获取音乐文件的一些数据信息(封装获取封面图片的类)
// // AVMetadataInfo.h // AVMetadata // // Created by Wengrp on 15/10/27. // Copyright © 2015年 Wengr ...
- winform app.config文件的动态配置
获取 获取应用程序exe.config文件中 节点value值 /// <summary> /// 功能: 读取应用程序exe.config文件中 /// appSettings节点下 ...
- 利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类
利用HttpWebRequest模拟表单提交 1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...
随机推荐
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
首先做出整个应用的全局请求队列 package com.qg.lizhanqi.myvolleydemo; import android.app.Application; import com.and ...
- node.js如何使用回调
Node.js到处使用回调,尤其在有I/O(输入/输出)操作的地方. 下面是在一个Node.js中使用filesystem模块中从磁盘上读入文件内容示例一: var fs = require('fs' ...
- hdu 1587
Problem Description As you know, Gardon trid hard for his love-letter, and now he's spending too muc ...
- OpenCV——人脸检测
OpenCV支持的目标检测的方法: 利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification) 1.加载级联分类器 CascadeClass ...
- 配置sphinx
1.先安装sphinxclient #cd /usr/local/src #wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz ...
- Python之路第十三天,高级(7)-详述数据库一对多,多对多表关系的设计以及如何查询
一对多表设计和查询方法 #!/usr/bin/env python3 # Author: Zhangxunan from sqlalchemy import create_engine from sq ...
- Leetcode 104 Maximum Depth of Binary Tree python
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- STL中vector,Map,Set的实现原理
vector的数据安排以及操作方式,与array非常类似,两者唯一的区别是空间运用的灵活性,array是静态空间,一旦配置了就不能改变,如果你想要大一点的空间,就必须首先配置一块新空间,然后将原来的元 ...
- 小希的迷宫(HDU 1272 并查集判断生成树)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- js去除左右空格
replace方法去掉字符串的空格 //去左空格; s=s.replace(/(^\s*)/g, ""); //去右空格; s= s.replace(/(\s*$)/g, &qu ...