public class ConfigUtils
{
public static string filename = System.Windows.Forms.Application.StartupPath + @"\App.config";
/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值,返回字符串
/// </summary>
/// <param name="key">要读取的Key值</param>
/// <returns>返回Value值的字符串</returns>
public static string GetAppSetting(string key)
{
string value = null;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
value = element.GetAttribute("value");
}
return value;
}
/// <summary>
/// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
/// </summary>
/// <param name="name">要读取的name值</param>
/// <returns>返回connectionString值的字符串</returns>
public static string GetConnectionString(string name)
{
string connectionString = null;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
connectionString = element.GetAttribute("connectionString");
}
return connectionString;
}
/// <summary>
/// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="key">子节点Key值</param>
/// <param name="value">子节点value值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetAppSetting(string key, string value)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
{
//不存在则新增appSettings子节点
XmlNode root = doc.DocumentElement;
XmlElement appElement = doc.CreateElement("appSettings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
appElement.AppendChild(subElement);
root.AppendChild(appElement);
}
else
{
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 e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetConnectionString(string name, string connectionString, string providerName)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
try
{
if (node == null)
{
//不存在则新增connectionStrings子节点
XmlNode root = doc.DocumentElement;
XmlElement connElement = doc.CreateElement("connectionStrings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
connElement.AppendChild(subElement);
root.AppendChild(connElement);
}
else
{
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 e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool SetConnectionString(string name, string connectionString)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
try
{
if (node == null)
{
//不存在则新增connectionStrings子节点
XmlNode root = doc.DocumentElement;
XmlElement connElement = doc.CreateElement("connectionStrings");
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
connElement.AppendChild(subElement);
root.AppendChild(connElement);
}
else
{
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则更新子节点
element.SetAttribute("connectionString", connectionString);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
node.AppendChild(subElement);
}
}
doc.Save(filename);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
} /// <summary>
/// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="key">要删除的子节点Key值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteAppSetting(string key)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//appSettings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (element != null)
{
//存在则删除子节点
element.ParentNode.RemoveChild(element);
}
try
{
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="name">要删除的子节点name值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteConnectionString(string name)
{
bool isSuccess = false;
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
if (element != null)
{
//存在则删除子节点
node.RemoveChild(element);
} try
{
doc.Save(filename);
isSuccess = true;
}
catch (Exception e)
{
isSuccess = false;
}
return isSuccess;
}
}
    class SetConfig
{
/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public static string GetAppSettings(string strKey)
{
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == strKey)
{
return ConfigurationManager.AppSettings[strKey];
}
}
return null;
}
/// <summary>
/// 更新或新增[appSettings]节点的子节点值
/// </summary>
/// <param name="newKey"></param>
/// <param name="newValue"></param>
public static void SetAppSettings(string newKey, string newValue)
{
bool isModified = false;
// 如果要更改的Key已经存在
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == newKey)
{
isModified = true;
}
}
//打开 App.Config
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
//添加新的配置到配置文件里
config.AppSettings.Settings.Add(newKey, newValue);
//保存对配置节所做的更改
config.Save(ConfigurationSaveMode.Modified);
//强制重载配置节
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 对[connectionStrings]节点依据name值读取
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public static string GetConnectionString(string strName)
{
foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
{
if (item.Name == strName)
{
return ConfigurationManager.ConnectionStrings[strName].ConnectionString;
}
}
return null;
}
/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值
/// </summary>
/// <param name="newName"></param>
/// <param name="newValue"></param>
public static void SetConnectionString(string newName, string newValue)
{
bool isModified = false;
// 如果要更改的Key已经存在
foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
{
if (item.Name == newName)
{
isModified = true;
}
}
//打开 App.Config
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//添加新的配置到配置文件里
ConnectionStringSettings conSetting = new ConnectionStringSettings(newName, newValue);
config.ConnectionStrings.ConnectionStrings.Add(conSetting);
//保存对配置节所做的更改
config.Save(ConfigurationSaveMode.Modified);
//强制重载配置节
ConfigurationManager.RefreshSection("connectionStrings");
} }

App.Config操作的更多相关文章

  1. c# 配置文件App.config操作类库

    public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </sum ...

  2. App.Config详解及读写操作

    App.Config详解及读写操作   App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...

  3. C#----操作应用程序配置文件App.config

    对配置文件的一些疑问: 在应用程序的目录下,有两处值得注意的地方,一个是应用程序根目录下的App.config文件,和bin\debug\name.exe.config 或者 bin\Release\ ...

  4. [转载]App.Config详解及读写操作

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

  5. 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作

    最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...

  6. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

    原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...

  7. .NET下对Web.config与App.Config的增删改操作的代码

    把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...

  8. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  9. (转)App.Config详解及读写操作

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

随机推荐

  1. python如何查看内存占用空间

    我们如何查看变量占用了多少内存空间呢 首先我们引用sys模块,在使用getsizeof()方法 import sys L = [x for x in range(10000)] print(sys.g ...

  2. 对props的研究

    Vue.component('my-component', { props: { // 基础的类型检查 (`null` 匹配任何类型) propA: Number, // 多个可能的类型 propB: ...

  3. python-列表元祖字典集合

    列表 list = ["a", "b", "c", "d"]元祖 tup = (1, 2, 3, 4, 5 ) 1.元组 ...

  4. JLRoutes笔记

    1.在info.plist中添加 <key>CFBundleURLTypes</key> <array> <dict> <key>CFBun ...

  5. 如何删除发布服务器distribution

    在建立发布服务器后自动生成distribution数据库为系统数据库,drop无法删除,实际删除方法如下:在“对象资源管理器”-“复制”上点击右键,选择“禁用发布和分发”,依次执行即可完成该系统数据库 ...

  6. MySQL server has gone away 问题解决方法

    问题描述: SQLyog在执行大的sql文件时候,报错,报错日志显示2006 - MySQL server has gone away 解决办法: 在php.ini配置文件的[mysqld]节点下添加 ...

  7. 二叉搜索树第k个节点

    /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...

  8. [CSP-S模拟测试]:题(DP)

    题目描述 由于出题人赶时间所以没办法编故事来作为背景.一开始有$n$个苹果,$m$个人依次来吃苹果,第$i$个人会尝试吃$u_i$或$v_i$号苹果,具体来说分三种情况.$\bullet 1.$两个苹 ...

  9. 后端技术杂谈9:先搞懂Docker核心概念吧

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  10. noi.ac #227 random

    分析 我们发现实际只要计算a[i]>b[j]和a[i]<b[j]哪种多即可 代码 #include<bits/stdc++.h> using namespace std; ], ...