技术开发 频道

对web.config进行增加修改删除的示例


【IT168技术文档】

  开发时,经常要动态的修改config文件,下面的就是一个实用类。
1using System; 2using System.Configuration; 3using System.Reflection; 4using System.Web; 5using System.Xml; 6public enum ConfigFileType 7{ 8 WebConfig, 9 AppConfig 10} 11 12namespace WebApplication1 13{ 14 /**//// <summary> 15 /// Summary description for ReadWriteConfig. 16 /// </summary> 17 public class ReadWriteConfig 18 { 19 public string docName = String.Empty; 20 private XmlNode node=null; 21 private int _configType; 22 public int ConfigType 23 { 24 get{ return _configType; } 25 set{ _configType=value; } 26 } 27 28 SetValue#region SetValue 29 public bool SetValue(string key, string value) 30 { 31 XmlDocument cfgDoc = new XmlDocument(); 32 loadConfigDoc(cfgDoc); 33 // retrieve the appSettings node 34 node = cfgDoc.SelectSingleNode("//appSettings"); 35 if( node == null ) 36 { 37 throw new InvalidOperationException( "appSettings section not found"); 38 } 39 try 40 { 41 // XPath select setting "add" element that contains this key 42 XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ; 43 if(addElem!=null) 44 { 45 addElem.SetAttribute("value",value); 46 } 47 // not found, so we need to add the element, key and value 48 else 49 { 50 XmlElement entry = cfgDoc.CreateElement("add"); 51 entry.SetAttribute("key",key); 52 entry.SetAttribute("value",value); 53 node.AppendChild(entry); 54 } 55 //save it 56 saveConfigDoc(cfgDoc,docName); 57 return true; 58 } 59 catch 60 { 61 return false; 62 } 63 } 64 65 #endregion 66 67 saveConfigDoc#region saveConfigDoc 68 private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath) 69 { 70 try 71 { 72 XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null ); 73 writer.Formatting = Formatting.Indented; 74 cfgDoc.WriteTo( writer ); 75 writer.Flush(); 76 writer.Close(); 77 return; 78 } 79 catch 80 { 81 throw; 82 } 83 } 84 85 #endregion 86 87 removeElement#region removeElement 88 public bool removeElement (string elementKey) 89 { 90 try 91 { 92 XmlDocument cfgDoc = new XmlDocument(); 93 loadConfigDoc(cfgDoc); 94 // retrieve the appSettings node 95 node = cfgDoc.SelectSingleNode("//appSettings"); 96 if( node == null ) 97 { 98 throw new InvalidOperationException( "appSettings section not found"); 99 } 100 // XPath select setting "add" element that contains this key to remove 101 node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") ); 102 saveConfigDoc(cfgDoc,docName); 103 return true; 104 } 105 catch 106 { 107 return false; 108 } 109 } 110 #endregion 111 112 modifyElement#region modifyElement 113 public bool modifyElement (string elementKey) 114 { 115 try 116 { 117 XmlDocument cfgDoc = new XmlDocument(); 118 loadConfigDoc(cfgDoc); 119 // retrieve the appSettings node 120 node = cfgDoc.SelectSingleNode("//appSettings"); 121 if( node == null ) 122 { 123 throw new InvalidOperationException( "appSettings section not found"); 124 } 125 // XPath select setting "add" element that contains this key to remove 126 node.RemoveChild(node.SelectSingleNode("//add[@key='" +elementKey +"']")); 127 saveConfigDoc(cfgDoc,docName); 128 return true; 129 } 130 catch 131 { 132 return false; 133 } 134 } 135 #endregion 136 137 loadConfigDoc#region loadConfigDoc 138 private XmlDocument loadConfigDoc( XmlDocument cfgDoc ) 139 { 140 // load the config file 141 if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig)) 142 { 143 docName= ((Assembly.GetEntryAssembly()).GetName()).Name; 144 docName += ".exe.config"; 145 } 146 else 147 { 148 docName=HttpContext.Current.Server.MapPath("web.config"); 149 } 150 cfgDoc.Load( docName ); 151 return cfgDoc; 152 } 153 #endregion 154 } 155}
0
相关文章