c# Config配置文件读写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions; namespace PrintLableCode
{
/// <summary>
/// 配置文件读写类
/// </summary>
public static class ConfigHelper
{
/// <summary>
/// 根据连接串名称connectionName返回连接字符串
/// </summary>
/// <param name="connectionName"></param>
/// <returns>connectionString</returns>
public static string GetConnectionStringsConfig(string connectionName)
{
//指定config文件读取
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
string connectionString =
config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
return connectionString;
}
/// <summary>
/// 更新连接字符串
/// </summary>
/// <param name="newName">连接字符串名称</param>
/// <param name="newConString">连接字符串内容</param>
/// <param name="newProviderName">数据提供程序名称</param>
public static void UpdateConnectionStringsConfig(string newName, string newConString,string newProviderName)
{
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(file); bool exist = false;//记录是否存在该连接串
if(config.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
//如果存在需要更改的连接串,先删除
if (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName,newConString,newProviderName);
//将新的连接串添加到配置文件中
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
//保存对配置文件的更改
config.Save(ConfigurationSaveMode.Modified);
//强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
}
/// <summary>
/// 返回*.exe.config文件中appSettings配置节的value项
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public static string GetAppConfig(string strKey)
{
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
foreach(string key in config.AppSettings.Settings.AllKeys)
{
if(key == strKey)
{
return config.AppSettings.Settings[strKey].Value.ToString();
}
}
return null;
}
///<summary>
///在*.exe.config文件中appSettings配置节增加一对键值对
///</summary>
///<param name="newKey"></param>
///<param name="newValue"></param>
public static void UpdateAppConfig(string newKey, string newValue)
{
string file = System.Windows.Forms.Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
bool exist = false;
foreach (string key in config.AppSettings.Settings.AllKeys)
{
if (key == newKey)
{
exist = true;
}
}
if (exist)
{
config.AppSettings.Settings.Remove(newKey);
}
config.AppSettings.Settings.Add(newKey, newValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
} }
c# Config配置文件读写的更多相关文章
- Config配置文件读写
config文件读写操作(文字说明附加在程序中) App.config文件 <?xml version="1.0" encoding="utf-8" ?& ...
- C#读写config配置文件
应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 对于一个config ...
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- Winform—C#读写config配置文件
现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager.并且AppSettings属性是只读的,并不支持修改属性值. 一.如 ...
- C#读写config配置文件--读取配置文件类
一:通过Key访问Value的方法: //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) ...
- .NET平台开源项目速览(1)SharpConfig配置文件读写组件
在.NET平台日常开发中,读取配置文件是一个很常见的需求.以前都是使用System.Configuration.ConfigurationSettings来操作,这个说实话,搞起来比较费劲.不知道大家 ...
随机推荐
- tftp-hpa客户端使用说明
1.板子 sudo apt-get install tftp-hpa 2.主机chmod 777 tftp—dir 3.tftp -4 192.168.1.122 -c put lib2.tar.gz ...
- Redis只作为缓存,不做持久化的配置
#1.配置缓存内存限制和清理策略 #作为缓存服务器,如果不加以限制内存的话,就很有可能出现将整台服务器内存都耗光的情况,可以在redis的配置文件里面设置: #example: # 限定最多使用1.5 ...
- Java研发书单
Java研发书单 计算机基础:<深入理解计算机系统><计算机网络> 网络方面:<TCP/IP协议卷一><unix网络编程卷一>(部分章节,JAVA主要是 ...
- 关于C++中不同类之间的赋值问题——存疑
operator=不能重载为全局函数.理由如下 void operator=(int i , A& a) { a.a = i } ; 那么将会出现 99 = a 这种代码,但是99不是左值, ...
- 【解决】SOUI向导生成项目(VC2013以上版本编译时)无法运行XP解决
对于SOUI向导生成的项目,无法在XP运行(提示 不是有效的WIN32程序 ) 即便设置为: 也无效,使用eXeScope打开发现最低系统要求是6.0,也就是说最少要WINXP以上,例如WIN7才能运 ...
- bbs3
第三天 昨日回顾: 1 验证码刷新 -$("#img_code")[0].src+="?" -本质就是向这个地址又发了一次请求 2 js中字符串拼接 -es5之 ...
- SQL虚拟数字辅助表
虚拟数字辅助表是一个整数序列,可以用来完成多种不同的任务,如生成日期跟时间值序列,及分裂值列表.要用查询逻辑产生一个大的整数序列,可以使用交叉连接(cross join). 交叉联接(cross jo ...
- myeclipse便捷导包方式
1.将spring框架的core导成如图的方式 2.选中项目右键--bulid path--进入buildpath 3.选择add library 4.选择user library 5.选择user ...
- (转)构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码
原文地址:http://www.cnblogs.com/ymnets/archive/2013/11/16/3426454.html 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间 ...
- pom.xml导入项目的时候,出错