【asp源码栏目提醒】:本文主要为网学会员提供“ASP 在程序中动态删除、修改配置文件节点值的方法-文学作品 - 讲义教程”,希望对需要ASP 在程序中动态删除、修改配置文件节点值的方法-文学作品 - 讲义教程网友有所帮助,学习一下!
技术派 当你越想纯粹的自我的过好每一天的时候周围功利的东西都会来影响你诱惑你。
要怎样坐怀不乱还是在乱与不乱的不断变换中体验成长的感觉
ASP. 在程序中动态删除、修改配置文件节点值的方法 第一步在App_Code文件夹下新建一个类ReadWriteConfig代码在博文后面可以完全复制 第二步如果在Web.config中有以下的节点及值可以按第三步中的方法进行操作 第三步使用ReadWriteConfig类进行操作 1修改节点值 bool b false //成功操作的返回值 ReadWriteConfig config new ReadWriteConfig b config.SetValueFileUploadSize 一个新值 2 删除节点 ReadWriteConfig config new ReadWriteConfig config.removeElementFileUploadType 3 查看节点值 ReadWriteConfig config new ReadWriteConfig SearchedValue config.readConfigDocFileUploadSize 搞定收工 有什么
问题的话可以直接留言哟…………………… ReadWriteConfig类的内容 using System using System.Configuration using System.Reflection using System.Web using System.Xml public enum ConfigFileType WebConfig AppConfig /// /// Summary description for ReadWriteConfig. /// public class ReadWriteConfig public string docName String.Empty private XmlNode node null private int _configType public int ConfigType get return _configType set _configType value region SetValue public bool SetValuestring key string value XmlDocument cfgDoc new XmlDocument loadConfigDoccfgDoc // retrieve the appSettings node node cfgDoc.SelectSingleNode//appSettings if node null throw new InvalidOperationExceptionappSettings section not found try // XPath select setting add element that contains this key XmlElement addElem XmlElementnode.SelectSingleNode//addkey key if addElem null addElem.SetAttributevalue value // not found so we need to add the element key and value else XmlElement entry cfgDoc.createElement_x_x_x_x_xadd entry.SetAttributekey key entry.SetAttributevalue value node.AppendChildentry //save it saveConfigDoccfgDoc docName return true catch return false endregion region saveConfigDoc private void saveConfigDocXmlDocument cfgDoc string cfgDocPath try XmlTextWriter writer new XmlTextWritercfgDocPath null writer.Formatting Formatting.Indented cfgDoc.WriteTowriter writer.Flush writer.Close return catch throw public string readConfigDocstring elementKey try XmlDocument cfgDoc new XmlDocument loadConfigDoccfgDoc // retrieve the appSettings node node cfgDoc.SelectSingleNode//appSettings if node null throw new InvalidOperationExceptionappSettings section not found XmlElement addElem XmlElementnode.SelectSingleNode//addkey elementKey if addElem null return addElem.GetAttributevalue // not found so we need to add the element key and value else return catch return endregion region removeElement public bool removeElementstring elementKey try XmlDocument cfgDoc new XmlDocument loadConfigDoccfgDoc // retrieve the appSettings node node cfgDoc.SelectSingleNode//appSettings if node null throw new InvalidOperationExceptionappSettings section not found // XPath select setting add element that contains this key to remove node.RemoveChildnode.SelectSingleNode//addkey elementKey saveConfigDoccfgDoc docName return true catch return false endregion region modifyElement public bool modifyElementstring elementKey try XmlDocument cfgDoc new XmlDocument loadConfigDoccfgDoc // retrieve the appSettings node node cfgDoc.SelectSingleNode//appSettings if node null throw new InvalidOperationExceptionappSettings section not found // XPath select setting add element that contains this key to remove node.RemoveChildnode.SelectSingleNode//addkey elementKey saveConfigDoccfgDoc docName return true catch return false endregion region loadConfigDoc private XmlDocument loadConfigDocXmlDocument cfgDoc // load the config file if Convert.ToInt32ConfigType Convert.ToInt32ConfigFileType.AppConfig docName Assembly.GetEntryAssembly.GetName.Name docName .exe.config else docName HttpContext.Current.Server.MapPath/Web.config //你的配置文件名称 cfgDoc.LoaddocName return cfgDoc endregion