对config文件中的配置进行新增修改删除读取操作
2006-03-19 16:10
951 查看
using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
WebConfig,
AppConfig
}
namespace WebApplication1
{
/**//// <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; }
}
SetValue#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("add");
entry.SetAttribute("key",key);
entry.SetAttribute("value",value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}
#endregion
saveConfigDoc#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;
}
}
#endregion
removeElement#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
modifyElement#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
loadConfigDoc#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
}
}
相关文章推荐
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- [转]对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对appSettings节点进行新增、修改、删除、读取操作的类AppSettingsHelper
- 配置文件操作.如config.php文件的读取修改等操作
- Java读取json文件并对json数据进行读取、添加、删除与修改操作
- 如何读取配置文件并连接后台数据库,且对数据库进行操作实例(查询或修改等)
- php中配置文件操作 如config.php文件的读取修改等操作