Runtime Web.config / App.config Editing
2005-07-05 17:19
405 查看
摘自http://www.eggheadcafe.com/articles/20030907.asp
using System; using System.Xml; using System.Configuration; using System.Collections; using System.Reflection; using System.Diagnostics ; public enum ConfigFileType { WebConfig , AppConfig } public class AppConfig : System.Configuration.AppSettingsReader { public string docName = String.Empty; private XmlNode node=null; private int _configType; public int ConfigType { get { return _configType; } set { _configType=value; } } public 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 System.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; } } private 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; } } public bool removeElement ( string elementKey) { try { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if( node == null ) { throw new System.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 XmlDocument loadConfigDoc( XmlDocument cfgDoc ) { // load the config file if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig)) { docName= ((Assembly.GetEntryAssembly()).GetName()).Name; docName += ".exe.config"; } else { docName=System.Web.HttpContext.Current.Server.MapPath("web.config"); } cfgDoc.Load( docName ); return cfgDoc; } }
相关文章推荐
- Runtime Web.config / App.config Editing
- Runtime Web.config / App.config Editing
- Runtime Web.config / App.config Editing
- 在web.config和app.config文件中增加自定义配置节点
- web.config中httpRunTime的属性 .
- 在Web.config或App.config中的添加自定义配置
- App.config/Web.config 中特殊字符的处理
- 读取、添加、删除、修改配置文件 如(Web.config, App.config)
- silverlight 实现 读取app.config 或 web.config(转)
- C#对App.config文件或者web.config文件中节点的操作类
- web.config/app.config敏感数据加/解密的二种方法
- 读取webconfig里面的appSetting和connectionString
- 【转载】App.config/Web.config 中特殊字符的处理
- 加密App.Config文件和Web.Config
- 配置文件——WebApp.config文件读取和修改
- ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
- 动态修改App.Config 和web.Config
- .net分布在指定文件夹的web.confgi或者app.config
- web.config中httpRunTime的属性
- 在web.config文件中,增加“type="APP.Modules.CommandModule,CommandModules"”节点会导致awesome font字体图标显示为方框框