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文件操作的更多相关文章

  1. winform IO文件操作

    最近做了一个查错工具,运用了winform文件操作的知识,做了几点总结,不全面,只总结了几点项目里用过的知识(关于以下内容只是个人的理解和总结,不对的地方请多指教,有补充的可以评论留言大家一起讨论学习 ...

  2. winform INI文件操作辅助类

    using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...

  3. 修改Config文件

    /// <summary> /// Config文件操作 /// </summary> public class Config { /// <summary> // ...

  4. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  5. C#操作config文件

    以下是app.config或web.config的定义,定义了一个参数,键为Isinit,值为false <?xml version="1.0"?> <confi ...

  6. .NET开发笔记--对config文件的操作(1)

    1先写一些常用的公共类: 在Web.config文件中的配置: <!-- appSettings网站信息配置--> <appSettings> <add key=&quo ...

  7. C# 操作自定义config文件

    示例文件:DB.config 1.读取 //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中: ExeConfigura ...

  8. C#简单操作app.config文件

    即将操作的app.config文件内容如下 <?xml version="1.0" encoding="utf-8"?> <configura ...

  9. winform客户端程序实时读写app.config文件

    新接到需求,wcf客户端程序运行时,能实时修改程序的打印机名称: 使用XmlHelper读写 winform.exe.config文件修改后始终,不能实时读取出来,查询博客园,原来已有大神解释了: 获 ...

随机推荐

  1. xUtils的介绍

    鉴于大家的热情,我写了一篇Android 最火框架XUtils之注解机制详解<-点击查看 xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal ...

  2. (圆形imageview 类似qq头像)---》(ps:引用第三库APAvatarImageView>

    使用方法:在故事版上把imageview的class名称设置为APAvatarImageView就可以显示圆形图,其他相关属性可以参考此类. demo下载地址 https://github.com/a ...

  3. Nodejs新建博客练习(二)添加flash支持

    安装必须模块 npm install connect-flash npm install express-session 然后在app.js里面添加一些代码 var flash = require(' ...

  4. 【阿里云产品公测】消息队列服务MQS使用分享

    作者:阿里云用户 wiwi 消息队列MQS,顾名思义,是用于发送接收消息用的.废话不说,直接进入主题. 使用场景:服务添加了一个新功能,主要用于生成图片,本人用的开发语言是PHP,生成图片比较耗服务器 ...

  5. Java异常的一个小知识

    有以下两个代码: package com.lk.A; public class Test3 { public static void main(String[] args) { try { int a ...

  6. 《UNIX环境高级编程》学习心得 二

    窝萌来看我们看到这本书里的第一个程序 #include "apue.h" #include <dirent.h> int main(int argc, char *ar ...

  7. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  8. codeforces 680B B. Bear and Finding Criminals(水题)

    题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #includ ...

  9. 转: 跨终端Web之Hybrid App

    转:  http://www.infoq.com/cn/articles/hybrid-app 编者按:InfoQ开设新栏目“品味书香”,精选技术书籍的精彩章节,以及分享看完书留下的思考和收获,欢迎大 ...

  10. 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件

    我的情况如下: 在TabbarViewController中隐藏了系统的tabbar,然后自定义tabbar,A B C D 4个视图都有UINavigationController,A视图 使用的是 ...