App.Config操作
- 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操作的更多相关文章
- c# 配置文件App.config操作类库
public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </sum ...
- App.Config详解及读写操作
App.Config详解及读写操作 App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...
- C#----操作应用程序配置文件App.config
对配置文件的一些疑问: 在应用程序的目录下,有两处值得注意的地方,一个是应用程序根目录下的App.config文件,和bin\debug\name.exe.config 或者 bin\Release\ ...
- [转载]App.Config详解及读写操作
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings] ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...
- .NET下对Web.config与App.Config的增删改操作的代码
把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- (转)App.Config详解及读写操作
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...
随机推荐
- Jenkins插件--通知Notification
参考来源:http://blog.csdn.net/wangmuming/article/details/22925357 ============================ 题外话 邮箱配置需 ...
- SQL Server数据库的软硬件性能瓶颈
在过去十年里,很多复杂的企业应用都是用Microsoft SQL Server进行开发和部署的.如今,SQL Server已经成为现代业务应用的基石,并且它还是很多大公司业务流程的核心.SQL Ser ...
- php linux环境安装ftp扩展
1.进入PHP安装源码包,找到ext下的ftp,进入 cd /home/local/php-5.6.25/ext/ftp 2./usr/local/php/bin/phpize 3../configu ...
- 攻防世界 | guess_num
查源码发现v5可以覆盖sreed[0],这样这个随机数列就可控了 在随机种子这里我一开始使用devC++来生成,居然结果跟gcc的不一样
- instagram
https://www.instagram.com/graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={ ...
- LintCode之两两交换链表中的节点
题目描述: 我的思路: 由题目描述可知,题目是要求将第一个与第二个节点,第三个与第四节点....进行交换,而进行交换时只用将节点的值进行交换即可.需要注意的是:当链表为null或者当链表只有一个节点时 ...
- HDU 5632 Rikka with Array [想法题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5632 ------------------------------------------------ ...
- 仅对原表新增列的全量数据.csv
w
- 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型
前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...
- 将python文件打包成exe可执行文件
操作系统:win8-64位 python版本:3.5 pyInstaller版本:3.2(下载地址:http://www.pyinstaller.org/) pywin32版本:pywin32-219 ...