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. 来自 Thoughtram 的 Angular 2 系列资料

    Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...

  2. Oracle去掉字符串首尾

    今天刚注册博客,与大家分享一下今天的新的: 今天在报表中碰到这样一个需求,数据库里面的一个字段是其他的3个字段合成的,但是现在读取数据只要中间的那一部分, 思考了许久这个字段的中间部分不是固定的,头和 ...

  3. XMLHttpRequest简单总结

    一.概念 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据 在页面已加载后从 ...

  4. @ResponseBody

    @Controller public class PersonController { /** * 查询个人信息 * * @param id * @return */ @RequestMapping( ...

  5. 扩展easyui.datagrid,添加数据loading遮罩效果代码 --来自网摘收集

    //jquery.datagrid 扩展 (function (){ $.extend($.fn.datagrid.methods, { //显示遮罩 loading: function(jq){ r ...

  6. Action向前台输出

    import java.io.IOException;import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse ...

  7. [转载:]C#与Fortran混合编程之本地调用Fortran动态链接库

    前言 C#发展到现在,已是一门相当完善的语言,他基于C语言风格,演化于C++.并依靠强大的.NET底层框架.C#可以用来快速构建桌面及Web应用.然而在我们的实际工作中,尽管C#已经非常完善,但还是不 ...

  8. 一键安装lamp环境 centos

    linux centos yum安装LAMP环境 /*************链接**************/http://www.cnblogs.com/suger/p/3832093.html ...

  9. 自己用C语言写单片机PIC16 serial bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 为什么自己写bootl ...

  10. Word设置首页不同

    Sub 批量格式设置()  '此代码为指定文件夹中所有选取的WORD文件的进行格式设置 Dim MyDialog As FileDialog, vrtSelectedItem As Variant, ...