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来操作,这个说实话,搞起来比较费劲.不知道大家 ...
随机推荐
- 洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- [Excel]鼠标右键菜单没有新建Word、Excel、PPT怎么办?
很多朋友在安装好Office(2010或2013等)之后,发现右键新建中没有Word.Excel.PowerPoint等项,但是自己的Office却明明安装好了.这个时候该怎么办呢?这里,本文为大家提 ...
- input框添加阴影效果
.input:hover,.input:focus{ border:1px solid #85afe1; -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0 ...
- 01 A Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
- xfce4快捷键设置
xfce4的"Keyboard"可以方便的设置启动应用程序的快捷键. 例如,添加xfce4-terminal和emacs的启动快捷键 Alt+F3打开"Applicati ...
- ArcGIS 桌面远程连接带有端口号的SDE
首先配置远程连接 PostgreSQL数据库远程连接功能的开启 需要修改连个配置文件,默认位于 安装目录的data子文件夹下. 1.postgresql.conf 修改成监听所有ip地址的连接 ...
- APUE(8)---进程控制(1)
一.进程标识 每个进程都有一个非负整型标识的唯一进程ID.因为进程ID标识符总是唯一的,常将其用做其他标识符的一部分以保证其唯一性.进程ID虽然是唯一的, 但是却是可以复用的.ID为0的进程通常是调度 ...
- MAX6675操作源码--K型热电偶模数转换器
#define P_TENB PF4_OUT #define P_TSLK PA3_OUT #define P_TDAT PB2_IN //****************************** ...
- Reporting Service服务SharePoint集成模式安装配置(8、配置用于SharePoint 2010的Reporting service模式)
从SQL Server 2012 起, SQL Server Reporting Service可以完全集成进SharePoint的场,直接作为SharePoint 的组件部分来运行,没有独立的Win ...
- Android 画闹钟
1.今天就来模仿一下这个小闹钟的 2.思路: 先画闹钟的圆盘 ,在通过Path来画指针 两个耳朵其实就是用两个圆被一个大圆截取后留下的,并旋转一定度数后生成 3.直接上代码: public class ...