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配置文件读写的更多相关文章

  1. Config配置文件读写

    config文件读写操作(文字说明附加在程序中) App.config文件 <?xml version="1.0" encoding="utf-8" ?& ...

  2. C#读写config配置文件

    应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 对于一个config ...

  3. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  4. C#中动态读写App.config配置文件

    转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...

  5. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  6. 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  7. Winform—C#读写config配置文件

    现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager.并且AppSettings属性是只读的,并不支持修改属性值. 一.如 ...

  8. C#读写config配置文件--读取配置文件类

    一:通过Key访问Value的方法: //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) ...

  9. .NET平台开源项目速览(1)SharpConfig配置文件读写组件

    在.NET平台日常开发中,读取配置文件是一个很常见的需求.以前都是使用System.Configuration.ConfigurationSettings来操作,这个说实话,搞起来比较费劲.不知道大家 ...

随机推荐

  1. 10个android开发必备的开源项目

    You are here: Home » » Blog » 10 Open Source Android Apps which every Android developer must look in ...

  2. 设计模式--适配器模式(Adapter)详解

    适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式. 01.类的适配器模式 核心 ...

  3. 往数据库添加的时候(只添加以前未添加的记录的写法)c#

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. zigbee初探

    什么是zigbee? 1.它是一种通信方式,一种通信协议: 2.其作用就是构建一个类似无线局域网的东西:如果这个局域网用于传感器的数据收集.监控,那么这个网络就叫做无线传感器网络. 应用领域:家居.工 ...

  5. Selenium运用-漫画批量下载

    今天我们要爬去的网站是http://comic.sfacg.com/.漫画网站一般都是通过JavaScript和AJAX来动态加载漫画的,这也就意味着想通过原来爬取静态网站的方式去下载漫画是不可能的, ...

  6. [GO]revoer的应用

    error的函数只是用来报一些低等级的错误,panic是报那些会导致程序崩溃的错误,但是会有一个问题就是panic也会导致程序中断 ,如果我们需要程序在报错之后继续运行并报出错误的信息 就需要使用到r ...

  7. CEPH集群RBD快照创建、恢复、删除、克隆(转)

    Ceph支持一个非常好的特性,以COW(写时复制)的方式从RBD快照创建克隆,在Ceph中被称为快照分层.分层特性允许用户创建多个CEPH RBD克隆实例.这些特性应用于OpenStack等云平台中 ...

  8. Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)

    方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)

  9. python读取pop3服务器邮件并且下载

    # -*- coding: cp936 -*- import poplib import random import os def getmail(): # 蒋辉文拥有该程序权利 你可以随意使用 em ...

  10. MVC4 4种Filter

    1. AuthorizationFilter: 从命名上看这个用于完成授权相关的工作. AuthorizationFilter 实现了 IAuthorizationFilter 接口, 如果我们希望执 ...