1. public class ConfigUtils
  2. {
  3. public static string filename = System.Windows.Forms.Application.StartupPath + @"\App.config";
  4. /// <summary>
  5. /// 对[appSettings]节点依据Key值读取到Value值,返回字符串
  6. /// </summary>
  7. /// <param name="key">要读取的Key值</param>
  8. /// <returns>返回Value值的字符串</returns>
  9. public static string GetAppSetting(string key)
  10. {
  11. string value = null;
  12. XmlDocument doc = new XmlDocument();
  13. doc.Load(filename);
  14. XmlNode node = doc.SelectSingleNode("//appSettings");
  15. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
  16. if (element != null)
  17. {
  18. value = element.GetAttribute("value");
  19. }
  20. return value;
  21. }
  22. /// <summary>
  23. /// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
  24. /// </summary>
  25. /// <param name="name">要读取的name值</param>
  26. /// <returns>返回connectionString值的字符串</returns>
  27. public static string GetConnectionString(string name)
  28. {
  29. string connectionString = null;
  30. XmlDocument doc = new XmlDocument();
  31. doc.Load(filename);
  32. XmlNode node = doc.SelectSingleNode("//connectionStrings");
  33. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
  34. if (element != null)
  35. {
  36. connectionString = element.GetAttribute("connectionString");
  37. }
  38. return connectionString;
  39. }
  40. /// <summary>
  41. /// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
  42. /// </summary>
  43. /// <param name="key">子节点Key值</param>
  44. /// <param name="value">子节点value值</param>
  45. /// <returns>返回成功与否布尔值</returns>
  46. public static bool SetAppSetting(string key, string value)
  47. {
  48. bool isSuccess = false;
  49. XmlDocument doc = new XmlDocument();
  50. doc.Load(filename);
  51. XmlNode node = doc.SelectSingleNode("//appSettings");
  52. try
  53. {
  54. if (node == null)
  55. {
  56. //不存在则新增appSettings子节点
  57. XmlNode root = doc.DocumentElement;
  58. XmlElement appElement = doc.CreateElement("appSettings");
  59. XmlElement subElement = doc.CreateElement("add");
  60. subElement.SetAttribute("key", key);
  61. subElement.SetAttribute("value", value);
  62. appElement.AppendChild(subElement);
  63. root.AppendChild(appElement);
  64. }
  65. else
  66. {
  67. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
  68. if (element != null)
  69. {
  70. //存在则更新子节点Value
  71. element.SetAttribute("value", value);
  72. }
  73. else
  74. {
  75. //不存在则新增子节点
  76. XmlElement subElement = doc.CreateElement("add");
  77. subElement.SetAttribute("key", key);
  78. subElement.SetAttribute("value", value);
  79. node.AppendChild(subElement);
  80. }
  81. }
  82. using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
  83. {
  84. xmlwriter.Formatting = Formatting.Indented;
  85. doc.WriteTo(xmlwriter);
  86. xmlwriter.Flush();
  87. }
  88. isSuccess = true;
  89. }
  90. catch (Exception e)
  91. {
  92. isSuccess = false;
  93. }
  94. return isSuccess;
  95. }
  96. /// <summary>
  97. /// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
  98. /// </summary>
  99. /// <param name="name">子节点name值</param>
  100. /// <param name="connectionString">子节点connectionString值</param>
  101. /// <param name="providerName">子节点providerName值</param>
  102. /// <returns>返回成功与否布尔值</returns>
  103. public static bool SetConnectionString(string name, string connectionString, string providerName)
  104. {
  105. bool isSuccess = false;
  106. XmlDocument doc = new XmlDocument();
  107. doc.Load(filename);
  108. XmlNode node = doc.SelectSingleNode("//connectionStrings");
  109. try
  110. {
  111. if (node == null)
  112. {
  113. //不存在则新增connectionStrings子节点
  114. XmlNode root = doc.DocumentElement;
  115. XmlElement connElement = doc.CreateElement("connectionStrings");
  116. XmlElement subElement = doc.CreateElement("add");
  117. subElement.SetAttribute("name", name);
  118. subElement.SetAttribute("connectionString", connectionString);
  119. subElement.SetAttribute("providerName", providerName);
  120. connElement.AppendChild(subElement);
  121. root.AppendChild(connElement);
  122. }
  123. else
  124. {
  125. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
  126. if (element != null)
  127. {
  128. //存在则更新子节点
  129. element.SetAttribute("connectionString", connectionString);
  130. element.SetAttribute("providerName", providerName);
  131. }
  132. else
  133. {
  134. //不存在则新增子节点
  135. XmlElement subElement = doc.CreateElement("add");
  136. subElement.SetAttribute("name", name);
  137. subElement.SetAttribute("connectionString", connectionString);
  138. subElement.SetAttribute("providerName", providerName);
  139. node.AppendChild(subElement);
  140. }
  141. }
  142. doc.Save(filename);
  143. isSuccess = true;
  144. }
  145. catch (Exception e)
  146. {
  147. isSuccess = false;
  148. }
  149. return isSuccess;
  150. }
  151. /// <summary>
  152. /// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
  153. /// </summary>
  154. /// <param name="name">子节点name值</param>
  155. /// <param name="connectionString">子节点connectionString值</param>
  156. /// <param name="providerName">子节点providerName值</param>
  157. /// <returns>返回成功与否布尔值</returns>
  158. public static bool SetConnectionString(string name, string connectionString)
  159. {
  160. bool isSuccess = false;
  161. XmlDocument doc = new XmlDocument();
  162. doc.Load(filename);
  163. XmlNode node = doc.SelectSingleNode("//connectionStrings");
  164. try
  165. {
  166. if (node == null)
  167. {
  168. //不存在则新增connectionStrings子节点
  169. XmlNode root = doc.DocumentElement;
  170. XmlElement connElement = doc.CreateElement("connectionStrings");
  171. XmlElement subElement = doc.CreateElement("add");
  172. subElement.SetAttribute("name", name);
  173. subElement.SetAttribute("connectionString", connectionString);
  174. connElement.AppendChild(subElement);
  175. root.AppendChild(connElement);
  176. }
  177. else
  178. {
  179. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
  180. if (element != null)
  181. {
  182. //存在则更新子节点
  183. element.SetAttribute("connectionString", connectionString);
  184. }
  185. else
  186. {
  187. //不存在则新增子节点
  188. XmlElement subElement = doc.CreateElement("add");
  189. subElement.SetAttribute("name", name);
  190. subElement.SetAttribute("connectionString", connectionString);
  191. node.AppendChild(subElement);
  192. }
  193. }
  194. doc.Save(filename);
  195. isSuccess = true;
  196. }
  197. catch (Exception e)
  198. {
  199. isSuccess = false;
  200. }
  201. return isSuccess;
  202. }
  203.  
  204. /// <summary>
  205. /// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
  206. /// </summary>
  207. /// <param name="key">要删除的子节点Key值</param>
  208. /// <returns>返回成功与否布尔值</returns>
  209. public static bool DeleteAppSetting(string key)
  210. {
  211. bool isSuccess = false;
  212. XmlDocument doc = new XmlDocument();
  213. doc.Load(filename);
  214. XmlNode node = doc.SelectSingleNode("//appSettings");
  215. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
  216. if (element != null)
  217. {
  218. //存在则删除子节点
  219. element.ParentNode.RemoveChild(element);
  220. }
  221. try
  222. {
  223. using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
  224. {
  225. xmlwriter.Formatting = Formatting.Indented;
  226. doc.WriteTo(xmlwriter);
  227. xmlwriter.Flush();
  228. }
  229. isSuccess = true;
  230. }
  231. catch (Exception e)
  232. {
  233. isSuccess = false;
  234. }
  235. return isSuccess;
  236. }
  237. /// <summary>
  238. /// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
  239. /// </summary>
  240. /// <param name="name">要删除的子节点name值</param>
  241. /// <returns>返回成功与否布尔值</returns>
  242. public static bool DeleteConnectionString(string name)
  243. {
  244. bool isSuccess = false;
  245. XmlDocument doc = new XmlDocument();
  246. doc.Load(filename);
  247. XmlNode node = doc.SelectSingleNode("//connectionStrings");
  248. XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
  249. if (element != null)
  250. {
  251. //存在则删除子节点
  252. node.RemoveChild(element);
  253. }
  254.  
  255. try
  256. {
  257. doc.Save(filename);
  258. isSuccess = true;
  259. }
  260. catch (Exception e)
  261. {
  262. isSuccess = false;
  263. }
  264. return isSuccess;
  265. }
  266. }
  1. class SetConfig
  2. {
  3. /// <summary>
  4. /// 对[appSettings]节点依据Key值读取到Value值
  5. /// </summary>
  6. /// <param name="strKey"></param>
  7. /// <returns></returns>
  8. public static string GetAppSettings(string strKey)
  9. {
  10. foreach (string key in ConfigurationManager.AppSettings)
  11. {
  12. if (key == strKey)
  13. {
  14. return ConfigurationManager.AppSettings[strKey];
  15. }
  16. }
  17. return null;
  18. }
  19. /// <summary>
  20. /// 更新或新增[appSettings]节点的子节点值
  21. /// </summary>
  22. /// <param name="newKey"></param>
  23. /// <param name="newValue"></param>
  24. public static void SetAppSettings(string newKey, string newValue)
  25. {
  26. bool isModified = false;
  27. // 如果要更改的Key已经存在
  28. foreach (string key in ConfigurationManager.AppSettings)
  29. {
  30. if (key == newKey)
  31. {
  32. isModified = true;
  33. }
  34. }
  35. //打开 App.Config
  36. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  37. // 如果连接串已存在,首先删除它
  38. if (isModified)
  39. {
  40. config.AppSettings.Settings.Remove(newKey);
  41. }
  42. //添加新的配置到配置文件里
  43. config.AppSettings.Settings.Add(newKey, newValue);
  44. //保存对配置节所做的更改
  45. config.Save(ConfigurationSaveMode.Modified);
  46. //强制重载配置节
  47. ConfigurationManager.RefreshSection("appSettings");
  48. }
  49. /// <summary>
  50. /// 对[connectionStrings]节点依据name值读取
  51. /// </summary>
  52. /// <param name="strName"></param>
  53. /// <returns></returns>
  54. public static string GetConnectionString(string strName)
  55. {
  56. foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
  57. {
  58. if (item.Name == strName)
  59. {
  60. return ConfigurationManager.ConnectionStrings[strName].ConnectionString;
  61. }
  62. }
  63. return null;
  64. }
  65. /// <summary>
  66. /// 更新或新增[connectionStrings]节点的子节点值
  67. /// </summary>
  68. /// <param name="newName"></param>
  69. /// <param name="newValue"></param>
  70. public static void SetConnectionString(string newName, string newValue)
  71. {
  72. bool isModified = false;
  73. // 如果要更改的Key已经存在
  74. foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
  75. {
  76. if (item.Name == newName)
  77. {
  78. isModified = true;
  79. }
  80. }
  81. //打开 App.Config
  82. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  83. // 如果连接串已存在,首先删除它
  84. if (isModified)
  85. {
  86. config.ConnectionStrings.ConnectionStrings.Remove(newName);
  87. }
  88. //添加新的配置到配置文件里
  89. ConnectionStringSettings conSetting = new ConnectionStringSettings(newName, newValue);
  90. config.ConnectionStrings.ConnectionStrings.Add(conSetting);
  91. //保存对配置节所做的更改
  92. config.Save(ConfigurationSaveMode.Modified);
  93. //强制重载配置节
  94. ConfigurationManager.RefreshSection("connectionStrings");
  95. }
  96.  
  97. }

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. Jenkins插件--通知Notification

    参考来源:http://blog.csdn.net/wangmuming/article/details/22925357 ============================ 题外话 邮箱配置需 ...

  2. SQL Server数据库的软硬件性能瓶颈

    在过去十年里,很多复杂的企业应用都是用Microsoft SQL Server进行开发和部署的.如今,SQL Server已经成为现代业务应用的基石,并且它还是很多大公司业务流程的核心.SQL Ser ...

  3. php linux环境安装ftp扩展

    1.进入PHP安装源码包,找到ext下的ftp,进入 cd /home/local/php-5.6.25/ext/ftp 2./usr/local/php/bin/phpize 3../configu ...

  4. 攻防世界 | guess_num

    查源码发现v5可以覆盖sreed[0],这样这个随机数列就可控了 在随机种子这里我一开始使用devC++来生成,居然结果跟gcc的不一样

  5. instagram

    https://www.instagram.com/graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={ ...

  6. LintCode之两两交换链表中的节点

    题目描述: 我的思路: 由题目描述可知,题目是要求将第一个与第二个节点,第三个与第四节点....进行交换,而进行交换时只用将节点的值进行交换即可.需要注意的是:当链表为null或者当链表只有一个节点时 ...

  7. HDU 5632 Rikka with Array [想法题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5632 ------------------------------------------------ ...

  8. 仅对原表新增列的全量数据.csv

    w

  9. 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型

    前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...

  10. 将python文件打包成exe可执行文件

    操作系统:win8-64位 python版本:3.5 pyInstaller版本:3.2(下载地址:http://www.pyinstaller.org/) pywin32版本:pywin32-219 ...