ConfigHelper.cs
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的更多相关文章
- RedisRepository分享和纠错
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- 干货:.net core实现读取自定义配置文件,有源代码哦
看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息.这一篇教大家自定义配置文件: 1.在项目下创建配置文件 { & ...
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结
Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...
- 从java文件和CS文件里查询方法使用次数工具
前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...
- 关于 WP 开发中.xaml 与.xaml.cs 的关系
今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...
- .net 用户控件ascx.cs注册js脚本代码无效果
在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...
- DateHelper.cs日期时间操作辅助类C#
//==================================================================== //** Copyright © classbao.com ...
- 仅用aspx文件实现Ajax调用后台cs程序。(实例)
仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...
随机推荐
- CString std::string相互转换
CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...
- TableViewCell,TableView,UITableViewCell
这次的学习是在Navigation-based Application模板中,用RootViewController class设置操作方法,使用UITableView的属性值.在导航控制器控件为程序 ...
- JAVA 多线程和并发学习笔记(三)
Java并发编程中使用Executors类创建和管理线程的用法 1.类 Executors Executors类可以看做一个“工具类”.援引JDK1.6 API中的介绍: 此包中所定义的 Execut ...
- js 实现动态的图片时钟
效果如下图 附件有图片 http://files.cnblogs.com/files/biyongyao/时钟.rar 源代码 <!DOCTYPE html> <html> ...
- OpenWrt自定义和官方一样的固件
我用的OpenWrt版本是Barrier Breaker 14.07,硬件是NetGear WNDR4300. 我自定义固件的目的是把固件的根分区扩到最大(100MB,总FLASH是128MB),试过 ...
- clientTop、offsetTop和scrollTop的区分
页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...
- loaded the "ViewController" nib but the view outlet was not set.'
错误代码: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[U ...
- spring 注入静态变量
import java.util.Iterator; import java.util.LinkedList; import javax.annotation.PostConstruct; impor ...
- windows脚本配置ip地址
背景:工作上经常涉及到要调试设备,每次都要手动配置静态ip地址,配置完之后还要重新改回来,有时候为了连续调试多台设备,来回手动更改ip,实在麻烦. 思考:想到windows有脚本,可以利用脚本文件达到 ...
- xml对象的序列化和反序列化
对象序列化: /// <summary> /// 将一个对象序列化为XML字符串 /// </summary> /// <par ...