最近我做的一些项目,经常需要用到对应用程序的配置文件操作,如app.config和web.config的配置文件,特别是对配置文件中的[appSettings]和[connectionStrings]两个节点常常进行新增、修改、删除和读取相关的操作的,所以,我自己就亲手把这些相关的操作都封装到一个配置文件管理器中,用静态的方法来调用便可,以下是我的实现,以资参考.
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Collections.Generic; 6 using System.Text; 7 using System.Xml; 8 9 public enum ConfigurationFile 10 { 11 AppConfig=1, 12 WebConfig=2 13 } 14 15 /// <summary> 16 /// ConfigManager 应用程序配置文件管理器 17 /// </summary> 18 public class ConfigManager 19 { 20 public ConfigManager() 21 { 22 // 23 // TODO: 在此处添加构造函数逻辑 24 // 25 } 26 27 28 /// <summary> 29 /// 对[appSettings]节点依据Key值读取到Value值,返回字符串 30 /// </summary> 31 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 32 /// <param name="key">要读取的Key值</param> 33 /// <returns>返回Value值的字符串</returns> 34 public static string ReadValueByKey(ConfigurationFile configurationFile, string key) 35 { 36 string value = string.Empty; 37 string filename = string.Empty; 38 if (configurationFile.ToString()==ConfigurationFile.AppConfig.ToString()) 39 { 40 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 41 } 42 else 43 { 44 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 45 } 46 47 XmlDocument doc = new XmlDocument(); 48 doc.Load(filename); //加载配置文件 49 50 XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点 51 52 ////得到[appSettings]节点中关于Key的子节点 53 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); 54 55 if (element != null) 56 { 57 value = element.GetAttribute("value"); 58 } 59 60 return value; 61 } 62 63 /// <summary> 64 /// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串 65 /// </summary> 66 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 67 /// <param name="name">要读取的name值</param> 68 /// <returns>返回connectionString值的字符串</returns> 69 public static string ReadConnectionStringByName(ConfigurationFile configurationFile, string name) 70 { 71 string connectionString = string.Empty; 72 string filename = string.Empty; 73 if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString()) 74 { 75 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 76 } 77 else 78 { 79 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 80 } 81 82 XmlDocument doc = new XmlDocument(); 83 doc.Load(filename); //加载配置文件 84 85 XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[appSettings]节点 86 87 ////得到[connectionString]节点中关于name的子节点 88 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); 89 90 if (element != null) 91 { 92 connectionString = element.GetAttribute("connectionString"); 93 } 94 95 return connectionString; 96 } 97 98 /// <summary> 99 /// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值 100 /// </summary> 101 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 102 /// <param name="key">子节点Key值</param> 103 /// <param name="value">子节点value值</param> 104 /// <returns>返回成功与否布尔值</returns> 105 public static bool UpdateOrCreateAppSetting(ConfigurationFile configurationFile, string key, string value) 106 { 107 bool isSuccess = false; 108 string filename = string.Empty; 109 if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString()) 110 { 111 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 112 } 113 else 114 { 115 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 116 } 117 118 XmlDocument doc = new XmlDocument(); 119 doc.Load(filename); //加载配置文件 120 121 XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点 122 123 try 124 { 125 ////得到[appSettings]节点中关于Key的子节点 126 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); 127 128 if (element != null) 129 { 130 //存在则更新子节点Value 131 element.SetAttribute("value", value); 132 } 133 else 134 { 135 //不存在则新增子节点 136 XmlElement subElement = doc.CreateElement("add"); 137 subElement.SetAttribute("key", key); 138 subElement.SetAttribute("value", value); 139 node.AppendChild(subElement); 140 } 141 142 //保存至配置文件(方式一) 143 using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null)) 144 { 145 xmlwriter.Formatting = Formatting.Indented; 146 doc.WriteTo(xmlwriter); 147 xmlwriter.Flush(); 148 } 149 150 isSuccess = true; 151 } 152 catch (Exception ex) 153 { 154 isSuccess = false; 155 throw ex; 156 } 157 158 return isSuccess; 159 } 160 161 /// <summary> 162 /// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值 163 /// </summary> 164 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 165 /// <param name="name">子节点name值</param> 166 /// <param name="connectionString">子节点connectionString值</param> 167 /// <param name="providerName">子节点providerName值</param> 168 /// <returns>返回成功与否布尔值</returns> 169 public static bool UpdateOrCreateConnectionString(ConfigurationFile configurationFile, string name, string connectionString, string providerName) 170 { 171 bool isSuccess = false; 172 string filename = string.Empty; 173 if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString()) 174 { 175 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 176 } 177 else 178 { 179 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 180 } 181 182 XmlDocument doc = new XmlDocument(); 183 doc.Load(filename); //加载配置文件 184 XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点 185 186 try 187 { 188 ////得到[connectionStrings]节点中关于Name的子节点 189 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); 190 191 if (element != null) 192 { 193 //存在则更新子节点 194 element.SetAttribute("connectionString", connectionString); 195 element.SetAttribute("providerName", providerName); 196 } 197 else 198 { 199 //不存在则新增子节点 200 XmlElement subElement = doc.CreateElement("add"); 201 subElement.SetAttribute("name", name); 202 subElement.SetAttribute("connectionString", connectionString); 203 subElement.SetAttribute("providerName", providerName); 204 node.AppendChild(subElement); 205 } 206 207 //保存至配置文件(方式二) 208 doc.Save(filename); 209 isSuccess = true; 210 } 211 catch (Exception ex) 212 { 213 isSuccess = false; 214 throw ex; 215 } 216 return isSuccess; 217 } 218 219 /// <summary> 220 /// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值 221 /// </summary> 222 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 223 /// <param name="key">要删除的子节点Key值</param> 224 /// <returns>返回成功与否布尔值</returns> 225 public static bool DeleteByKey(ConfigurationFile configurationFile, string key) 226 { 227 bool isSuccess = false; 228 string filename = string.Empty; 229 if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString()) 230 { 231 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 232 } 233 else 234 { 235 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 236 } 237 XmlDocument doc = new XmlDocument(); 238 doc.Load(filename); //加载配置文件 239 XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点 240 ////得到[appSettings]节点中关于Key的子节点 241 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']"); 242 243 if (element != null) 244 { 245 //存在则删除子节点 246 element.ParentNode.RemoveChild(element); 247 } 248 else 249 { 250 //不存在 251 } 252 try 253 { 254 //保存至配置文件(方式一) 255 using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null)) 256 { 257 xmlwriter.Formatting = Formatting.Indented; 258 doc.WriteTo(xmlwriter); 259 xmlwriter.Flush(); 260 } 261 isSuccess = true; 262 } 263 catch (Exception ex) 264 { 265 isSuccess = false; 266 } 267 return isSuccess; 268 } 269 270 /// <summary> 271 /// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值 272 /// </summary> 273 /// <param name="configurationFile">要操作的配置文件名称,枚举常量</param> 274 /// <param name="name">要删除的子节点name值</param> 275 /// <returns>返回成功与否布尔值</returns> 276 public static bool DeleteByName(ConfigurationFile configurationFile, string name) 277 { 278 bool isSuccess = false; 279 string filename = string.Empty; 280 if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString()) 281 { 282 filename = System.Windows.Forms.Application.ExecutablePath + ".config"; 283 } 284 else 285 { 286 filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config"; 287 } 288 289 XmlDocument doc = new XmlDocument(); 290 doc.Load(filename); //加载配置文件 291 XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点 292 ////得到[connectionStrings]节点中关于Name的子节点 293 XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']"); 294 if (element != null) 295 { 296 //存在则删除子节点 297 node.RemoveChild(element); 298 } 299 else 300 { 301 //不存在 302 } 303 try 304 { 305 //保存至配置文件(方式二) 306 doc.Save(filename); 307 isSuccess = true; 308 } 309 catch (Exception ex) 310 { 311 isSuccess = false; 312 } 313 return isSuccess; 314 } 315 }