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. 针对“Can't download driver to specified address”错误

    当用h-flasher检测flash-id时,可能会出现上述错误,个人认为当提示这个错误时,一般来说是sdram出现了问题,可以看看sdram有没有虚焊,或者周围的电阻电容是否正确. 在后来调板子的过 ...

  2. 2.2 ARM处理器工作模式

    ARM Architecture Reference Manual Arm 指令框架手册 种工作模式 Processor mode Mode number Description User usr 0 ...

  3. OGC学习课程

    1.引言 由于项目需要,需要学习OGC相关地图标准,包括WMS.WFS.GML.SLD等,只是国内相关书籍大家都懂的,特向Google大师请教,得一秘籍<Open Web Mapping> ...

  4. AX7: Overlayering and extensions

    Customization: Overlayering and extensions https://ax.help.dynamics.com/en/wiki/customization-overla ...

  5. MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)

      孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...

  6. app 支付宝 支付 alipaySdk

    function pay(P1: JString; P2: Boolean): JString; cdecl;    function fetchOrderInfoFromH5PayUrl(P1: J ...

  7. 利用cubieboard设置samba打印服务器

    #注意安装下面软件前,先将cubieboard的动态地址改为静态地址! apt-get install samba #安装samba vi /etc/samba/smb.conf //配置 workg ...

  8. percona-toolkit工具包的安装和使用

    1.安装与Perl相关的模块 PT工具是使用Perl语言编写和执行的,所以需要系统中有Perl环境 # yum install -y perl perl-devel perl-Time-HiRes p ...

  9. mvc 导入excel表格

    <script> $(function () { $("#Attachment").change(function () { var att = $("#At ...

  10. QT中的C/S通信问题:关于服务器端readyread()信号不发射

    在windows下用QT测试C/S通信的时候,遇到服务器端不能读取客户端发来的数据的问题. 后来设置断点检查错误,发现是readyread信号不触发的原因. 后来在客户端写socket后面加了一句so ...