ASP.NET 在程序中动态删除、修改配置文件节点值的方法
2011-09-29 10:57
781 查看
第一步:在App_Code文件夹下新建一个类ReadWriteConfig,代码在博文后面,可以完全复制 第二步:如果在Web.config中有以下的节点及值,可以按第三步中的方法进行操作 <appSettings> <add key="FileUploadSize" value="10240" /> <add key="FileUploadType" value="JPG|GIF|PNG" />
</appSettings>
第三步:使用ReadWriteConfig类进行操作 (1)修改节点值 bool b = false; //成功操作的返回值
ReadWriteConfig config = new ReadWriteConfig();
b = config.SetValue("FileUploadSize", 一个新值); (2) 删除节点 ReadWriteConfig config = new ReadWriteConfig();
config.removeElement("FileUploadType"); (3) 查看节点值 ReadWriteConfig config = new ReadWriteConfig();
SearchedValue = config.readConfigDoc("FileUploadSize"); 搞定,收工!!!! 有什么问题的话可以直接留言哟…………………… ReadWriteConfig类的内容 using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
WebConfig,
AppConfig
} /// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>
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 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_x_x_x_x_x("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
} #endregion #region saveConfigDoc
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 string readConfigDoc(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");
}
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']"); if (addElem != null)
{
return addElem.GetAttribute("value");
}
// not found, so we need to add the element, key and value
else
{
return "";
}
}
catch
{
return "";
}
}
#endregion #region removeElement
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 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;
}
}
#endregion #region modifyElement
public 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;
}
}
#endregion #region loadConfigDoc
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 = HttpContext.Current.Server.MapPath("~/Web.config"); //你的配置文件名称
}
cfgDoc.Load(docName);
return cfgDoc;
}
#endregion
}
</appSettings>
第三步:使用ReadWriteConfig类进行操作 (1)修改节点值 bool b = false; //成功操作的返回值
ReadWriteConfig config = new ReadWriteConfig();
b = config.SetValue("FileUploadSize", 一个新值); (2) 删除节点 ReadWriteConfig config = new ReadWriteConfig();
config.removeElement("FileUploadType"); (3) 查看节点值 ReadWriteConfig config = new ReadWriteConfig();
SearchedValue = config.readConfigDoc("FileUploadSize"); 搞定,收工!!!! 有什么问题的话可以直接留言哟…………………… ReadWriteConfig类的内容 using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
WebConfig,
AppConfig
} /// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>
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 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_x_x_x_x_x("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
} #endregion #region saveConfigDoc
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 string readConfigDoc(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");
}
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']"); if (addElem != null)
{
return addElem.GetAttribute("value");
}
// not found, so we need to add the element, key and value
else
{
return "";
}
}
catch
{
return "";
}
}
#endregion #region removeElement
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 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;
}
}
#endregion #region modifyElement
public 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;
}
}
#endregion #region loadConfigDoc
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 = HttpContext.Current.Server.MapPath("~/Web.config"); //你的配置文件名称
}
cfgDoc.Load(docName);
return cfgDoc;
}
#endregion
}
相关文章推荐
- asp.net中动态修改配置文件的方法
- asp.net中动态修改配置文件的方法
- winform程序 和 asp.net 程序 动态修改配置文件
- asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- WinForm OR ASP.NET获取修改配置文件config的节点
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- Asp.net中C#增加删除查找与修改xml文件元素节点
- asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- asp.net中怎么动态修改配置文件
- asp.Net 读取,删除(包括父节点)配置文件
- Asp.net使用代码修改配置文件的节点值
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- [转]ASP.NET Web.config配置文件的基本使用方法
- IIS配置asp.net网站出现错误:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- ASP.NET 2.0中母版页中引用文件路径的问题(对原处理方法中的bug进行了修改)
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET配置文件中常用到的节点信息