winform开发中对App.Config的写和读操作
2012-05-20 11:17
260 查看
using System; using System.Configuration; using System.Collections.Generic; using System.Text; using System.Xml; using System.Reflection; namespace Common { /// <summary> /// 对AppSettings节点进行增加,删除,修改操作. /// </summary> public static class AppSettingsHelper { public static string docName = String.Empty; private static XmlNode node = null; /// <summary> /// 设置节点的值,若该节点不存在,则创建一个新的节点。 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="cfgType"></param> /// <returns></returns> public static bool SetValue(string key, string value) { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if (node == null) { throw new InvalidOperationException("appSettings section not found"); } try { // XPath select setting "add" element that contains this key XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); if (addElem != null) { addElem.SetAttribute("value", value); } // not found, so we need to add the element, key and value else { XmlElement entry = cfgDoc.CreateElement("add"); entry.SetAttribute("key", key); entry.SetAttribute("value", value); node.AppendChild(entry); } //save it saveConfigDoc(cfgDoc, docName); return true; } catch { return false; } } /// <summary> /// 获取节点的值 /// </summary> /// <param name="key"></param> /// <param name="cfgType"></param> /// <returns></returns> public static string GetValue(string key) { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if (node == null) { throw new InvalidOperationException("appSettings section not found"); } // XPath select setting "add" element that contains this key XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); if (addElem != null) { return addElem.GetAttribute("value"); } // not found, so we need to add the element, key and value else { throw new ArgumentException(string.Format("key '{0}' not found", key)); } } private static void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath) { try { XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null); writer.Formatting = Formatting.Indented; cfgDoc.WriteTo(writer); writer.Flush(); writer.Close(); return; } catch { throw; } } /// <summary> /// 移除节点 /// </summary> /// <param name="elementKey"></param> /// <param name="cfgType"></param> /// <returns></returns> public static bool RemoveElement(string elementKey) { try { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if (node == null) { throw new InvalidOperationException("appSettings section not found"); } // XPath select setting "add" element that contains this key to remove node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']")); saveConfigDoc(cfgDoc, docName); return true; } catch { return false; } } /// <summary> /// 修改节点的值 /// </summary> /// <param name="elementKey"></param> /// <param name="cfgType"></param> /// <returns></returns> public static bool ModifyElement(string elementKey) { try { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if (node == null) { throw new InvalidOperationException("appSettings section not found"); } // XPath select setting "add" element that contains this key to remove node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']")); saveConfigDoc(cfgDoc, docName); return true; } catch { return false; } } private static XmlDocument loadConfigDoc(XmlDocument cfgDoc) { //docName = ((Assembly.GetEntryAssembly()).GetName()).Name; docName = @"G:\C#\test\FileOperate\FileOperate\App.config"; //docName += ".config"; cfgDoc.Load(docName); return cfgDoc; } } }
//调用
using Common;
......
//修改server的值,如果没有创建
bool b = AppSettingsHelper.SetValue("server", "adfwer");
//移除serverbool b = AppSettingsHelper.RemoveElement("server");
//获得server的值string str = AppSettingsHelper.GetValue("ConnectionString");
相关文章推荐
- .NET开发使用Winform的配置文件App.config
- C#WinForm APP.CONFIG配置文件的操作
- c# winform app.config xml 配置文件 读写操作
- Winform操作App.config(增加、修改、删除、读取等)
- winform中app.config的动态读写操作
- WPF中读写app.config操作
- 对WinForm的App.config文件进行加密
- Winform读取app.config文件
- Winform读写App.config文件以及重启程序
- vc++操作app.config
- winform程序读取和改写配置文件App.config元素的值
- ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
- winform中写app.config文件时调试情况下没有改变的原因
- app.config的节点AppSetting新增或修改操作
- c# WinForm开发 DataGridView控件的各种操作总结
- App.Config操作
- winform 程序的配置文件——App.config
- c#Winform程序调用app.config文件配置数据库连接字符串
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- 有关winform 配置文件 app.config