using System.Configuration;
using System.IO; /// <summary>
/// 配置文件辅助类
/// </summary>
public class ConfigHelper
{
/// <summary>
/// 获得指定配置节点的值
/// 节点不存在时返回 null
/// </summary>
public static string ReadAppConfig(string strKey)
{
if (ConfigurationManager.AppSettings[strKey] == null)
return null;
return ConfigurationManager.AppSettings[strKey].ToString();
} /// <summary>
/// 读 ConnectionStrings 节点 ConnectionName 的连接字符串
/// 节点不存在时返回 null
/// </summary>
public static string ReadConnectionStringConfig(string ConnectionName)
{
if (ConfigurationManager.ConnectionStrings[ConnectionName] == null)
return null;
return ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString.ToString();
} /// <summary>
/// 修改指定配置节点的值
/// 节点不存在则添加
/// </summary>
public static void WriteAppConfig(string newKey, string newValue)
{
// 打开配置文件
//Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[newKey] != null)
config.AppSettings.Settings.Remove(newKey);
// 添加新的键-值对
config.AppSettings.Settings.Add(newKey, newValue);
// 保存对配置文件的更改
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
} /// <summary>
/// 写 ConnectionStrings 节点 ConnectionName 的连接字符串
/// 节点不存在则添加
/// </summary>
public static void WriteConnectionStringConfig(string newName, string newConString, string newProvideName)
{
// 打开配置文件
//Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.ConnectionStrings[newName] != null)
config.ConnectionStrings.ConnectionStrings.Remove(newName);
// 添加新的连接字符串
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
// 保存对配置文件的更改
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("connectionStrings");
} /// <summary>
/// 重新读取配置文件
/// </summary>
public static void RefreshConfigs()
{
ConfigurationManager.RefreshSection("connectionStrings");
ConfigurationManager.RefreshSection("appSettings");
} // 2016年12月28日17时28分42秒 6 扩展,读其它配置文件
public static string ReadAppConfig(string filePath, string strKey)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.AppSettings.Settings[strKey] == null)
return null;
return cfg.AppSettings.Settings[strKey].Value;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
} public static string ReadConnectionStringConfig(string filePath, string ConnectionName)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.ConnectionStrings.ConnectionStrings[ConnectionName] == null)
return null;
return cfg.ConnectionStrings.ConnectionStrings[ConnectionName].ConnectionString;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
} public static void WriteAppConfig(string filePath, string newKey, string newValue)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.AppSettings.Settings[newKey] != null)
cfg.AppSettings.Settings.Remove(newKey);
cfg.AppSettings.Settings.Add(newKey, newValue);
// 保存对配置文件的更改
cfg.Save(ConfigurationSaveMode.Minimal);
return;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
} public static void WriteConnectionStringConfig(string filePath, string newName, string newConString, string newProvideName)
{
if(File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.ConnectionStrings.ConnectionStrings[newName] != null)
cfg.ConnectionStrings.ConnectionStrings.Remove(newName);
cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
// 保存对配置文件的更改
cfg.Save(ConfigurationSaveMode.Minimal);
return;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
}
}

部分内容摘自:http://www.jb51.net/article/34476.htm

ConfigHelper.cs的更多相关文章

  1. RedisRepository分享和纠错

    .mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...

  2. 干货:.net core实现读取自定义配置文件,有源代码哦

    看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息.这一篇教大家自定义配置文件: 1.在项目下创建配置文件 { & ...

  3. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  4. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  5. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  6. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

  7. .net 用户控件ascx.cs注册js脚本代码无效果

    在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...

  8. DateHelper.cs日期时间操作辅助类C#

    //==================================================================== //** Copyright © classbao.com ...

  9. 仅用aspx文件实现Ajax调用后台cs程序。(实例)

    仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...

随机推荐

  1. select问题总结

    select 从后台取来的值如何放到select里面的文本框中? $(".type option").each(function(){ if($(this).val() == de ...

  2. jquery 获取鼠标和元素的坐标点

    获取当前鼠标相对img元素的坐标 $('img').mousemove(function(e) { varpositionX=e.pageX-$(this).offset().left; //获取当前 ...

  3. Spark Streaming架构设计和运行机制总结

    本期内容 : Spark Streaming中的架构设计和运行机制 Spark Streaming深度思考 Spark Streaming的本质就是在RDD基础之上加上Time ,由Time不断的运行 ...

  4. jquery 上传回显图片预览

    /******************************************************************************* * 异步上传文件,兼容IE8,火狐和谷 ...

  5. 基于Python的函数回归算法验证

    看机器学习看到了回归函数,看了一半看不下去了,看到能用方差进行函数回归,又手痒痒了,自己推公式写代码验证: 常见的最小二乘法是一阶函数回归回归方法就是寻找方差的最小值y = kx + bxi, yiy ...

  6. shell命令快捷键

    在shell命令终端中,Ctrl+n相当于方向向下的方向键,Ctrl+p相当于方向向上的方向键.   在命令终端中通过它们或者方向键可以实现对历史命令的快速查找.这也是快速输入命令的技巧.   在命令 ...

  7. Debug 常见问题总结(持续更新)

    2016-9-24 1.for循环变量做参数一定要小心,嵌套一个for变量不要用同一个. 2.字符串处理要打好下标的草稿,不然很容易搞混.(方法待讨论). 3.整形比较比较容易忽略=的问题 ,> ...

  8. java学习第18天(map集合)

    Map集合是将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. 存储的是键值对形式的元素,键唯一,值可以重复,有点类似于数据库中的主键加数据.主要功能有: A:添加功能 put ...

  9. Access、Hybrid和Trunk

    以太网端口有三种链路类型:Access.Hybrid和Trunk.Access类型的端口只能属于1个VLAN,一般用于连接计算机的端口:Trunk类型的端口可以属于多个VLAN,可以接收和发送多个VL ...

  10. 自动检测浏览器是手机还是pc

    function CheckBrower() { if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad| ...