winform Config文件操作
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Reflection;
namespace ProcessErrorDataTools
{
public class ConfigManager
{
private static Configuration _configuration;
public static Configuration _Configuration
{
get
{
if (_configuration == null) _configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
return _configuration;
}
set { _configuration = value; }
}
public ConfigManager()
{
}
/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">要读取的Key值</param>
/// <returns>返回Value值的字符串</returns>
public static string ReadValueByKey(string key)
{
string value = string.Empty;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点
////得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
value = element.GetAttribute("value");
}
return value;
}
/// <summary>
/// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">要读取的name值</param>
/// <returns>返回connectionString值的字符串</returns>
public static string ReadConnectionStringByName(string name)
{
string connectionString = string.Empty;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[appSettings]节点
////得到[connectionString]节点中关于name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
connectionString = element.GetAttribute("connectionString");
}
return connectionString;
}
/// <summary>
/// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">子节点Key值</param>
/// <param name="value">子节点value值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool UpdateOrCreateAppSetting(string key, string value)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点
try
{
////得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
//存在则更新子节点Value
element.SetAttribute("value", value);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
node.AppendChild(subElement);
}
//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool UpdateOrCreateConnectionString(string name, string connectionString, string providerName)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点
try
{
////得到[connectionStrings]节点中关于Name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则更新子节点
element.SetAttribute("connectionString", connectionString);
element.SetAttribute("providerName", providerName);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
node.AppendChild(subElement);
}
//保存至配置文件(方式二)
doc.Save(filename);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}
return isSuccess;
}
/// <summary>
/// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">要删除的子节点Key值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteByKey(string key)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点
////得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
//存在则删除子节点
element.ParentNode.RemoveChild(element);
}
else
{
//不存在
}
try
{
//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">要删除的子节点name值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteByName(string name)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点
////得到[connectionStrings]节点中关于Name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则删除子节点
node.RemoveChild(element);
}
else
{
//不存在
}
try
{
//保存至配置文件(方式二)
doc.Save(filename);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}
}
}
winform Config文件操作的更多相关文章
- winform IO文件操作
最近做了一个查错工具,运用了winform文件操作的知识,做了几点总结,不全面,只总结了几点项目里用过的知识(关于以下内容只是个人的理解和总结,不对的地方请多指教,有补充的可以评论留言大家一起讨论学习 ...
- winform INI文件操作辅助类
using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...
- 修改Config文件
/// <summary> /// Config文件操作 /// </summary> public class Config { /// <summary> // ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
- C#操作config文件
以下是app.config或web.config的定义,定义了一个参数,键为Isinit,值为false <?xml version="1.0"?> <confi ...
- .NET开发笔记--对config文件的操作(1)
1先写一些常用的公共类: 在Web.config文件中的配置: <!-- appSettings网站信息配置--> <appSettings> <add key=&quo ...
- C# 操作自定义config文件
示例文件:DB.config 1.读取 //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中: ExeConfigura ...
- C#简单操作app.config文件
即将操作的app.config文件内容如下 <?xml version="1.0" encoding="utf-8"?> <configura ...
- winform客户端程序实时读写app.config文件
新接到需求,wcf客户端程序运行时,能实时修改程序的打印机名称: 使用XmlHelper读写 winform.exe.config文件修改后始终,不能实时读取出来,查询博客园,原来已有大神解释了: 获 ...
随机推荐
- Working with MTD Devices
转:http://www.linuxforu.com/2012/01/working-with-mtd-devices/ Working with MTD Devices By Mohan Lal J ...
- 解决faststone capture在台式机上录制屏幕视频没有声音的问题
点击屏幕录像机: 在弹出的屏幕录像机窗口中选择录制音频,然后点击选项按钮: 在弹出的选项按钮中选择视频选项,将所有的多选框都选中,注:最后一个选项录制windows XP的工具提示最好也选上,我发现如 ...
- ubuntu 设置IP,设置网关
1. 检验是否可以连通,就使用ping命令ping 网关 开始的时候总是现实unreachable 2. 设置IP sudo ifconfig eth0 133.133.133.190 netmask ...
- iOS---》点击uitableview 的section展开或隐藏
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...
- 自定义 404 与 500 错误页面,URL 地址不会重定向(一)
对于 404 与 500 错误发生时,我们希望自己定义一个更加人性化的页面. 例子 当访问下面这个地址时: http://localhost/aaaa/bbb/ccc/ddd/eee/fff/ggg ...
- Android(java)学习笔记263:Android下的属性动画(Property Animation)
1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...
- MySQL关于InnoDB的几个错误
阿里云服务器上装有MySQL 5.6,这几天MySQL服务经常死掉,启动MySQL服务(service mysql start),却报如下错误 Starting MySQL.. ERROR! The ...
- jQuery中json对象的复制(数组及对象) .
1.jQuery自带的$.map方式: $.map(json, function (n) { return n; }); 这种方式原来用于复制数组还可以,今天用它复制数组中的某一条记录,发现字段名称丢 ...
- oracle PL/SQL(procedure language/SQL)程序设计之函数+过程+包
匿名PL/SQL块回顾 DECLARE (可选) 定义在PL/SQL块中要使用的对象BEGIN (必须) 执行语句EXCEPTION (可选) 错误处理语句END; (必须)匿名块( ...
- ionic使用sass
sass 是一个css的预编译器,常见的预编译器有less,sass,stylus等,目前sass似乎更受青睐一些,bootstrap的最新版本以及ionic 都是用sass来构建页面效果的.这篇文章 ...