//
c#中对web.config配置文件的操作(增删改读)
using system;
using system.configuration;
using system.reflection;
using system.web;
using system.xml;
public enum configfiletype
... {
webconfig,
appconfig
}
namespace configrw
... {
/**//// <summary>
/// summary description for readwriteconfig.
/// </summary>
public class readwriteconfig
...{
public string docname = string.empty;
private xmlnode node=null;
private int _configtype;
public string message;
public int configtype
...{
get...{ return _configtype; }
set...{ _configtype=value; }
}
getvalue#region getvalue
public string getvalue( string key )
...{ //读取
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
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +key +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
message = system.configuration.configurationsettings.appsettings[key];
return message;
}
catch
...{
message = "操作异常,获取value值失败!";
return message;
}
}
#endregion
setvalue#region setvalue
public string 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)
...{
message = "此key已经存在!";
return message;
}
// 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);
message = "添加成功!";
return message;
}
catch
...{
message = "操作异常,添加失败!";
return message;
}
}
#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 string 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");
}
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
// xpath select setting "add" element that contains this key to remove
node.removechild( node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") );
saveconfigdoc(cfgdoc,docname);
message = "删除成功!";
return message;
}
catch
...{
message = "操作异常,删除失败!";
return message;
}
}
#endregion
modifyelement#region modifyelement
public string modifyelement (string elementkey, string elementvalue)
...{ //修改
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
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
addelem.setattribute("value",elementvalue);
//save it
saveconfigdoc(cfgdoc,docname);
message = "修改成功!";
return message;
}
catch
...{
message = "操作异常,修改失败!";
return message;
}
}
#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
}
}
测试页面
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace configrw
... {
/**//// <summary>
/// summary description for webform1.
/// </summary>
public class webform1 : system.web.ui.page
...{
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.label label1;
protected system.web.ui.webcontrols.textbox textbox1;
protected system.web.ui.webcontrols.label label2;
protected system.web.ui.webcontrols.textbox textbox2;
protected system.web.ui.webcontrols.button button3;
protected system.web.ui.webcontrols.button button4;
protected system.web.ui.webcontrols.button button2;
private void page_load(object sender, system.eventargs e)
...{
}
web form designer generated code#region web form designer generated code
override protected void oninit(eventargs e)
...{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
/**//// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
...{
this.button1.click += new system.eventhandler(this.button1_click);
this.button2.click += new system.eventhandler(this.button2_click);
this.button3.click += new system.eventhandler(this.button3_click);
this.button4.click += new system.eventhandler(this.button4_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void button2_click(object sender, system.eventargs e)
...{
//新增
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.setvalue(this.textbox1.text,this.textbox2.text));
}
private void button1_click(object sender, system.eventargs e)
...{
//删除
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.removeelement(this.textbox1.text));
}
private void button3_click(object sender, system.eventargs e)
...{
//修改
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.modifyelement(this.textbox1.text,this.textbox2.text));
}
private void button4_click(object sender, system.eventargs e)
...{
//读取
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
this.textbox2.text = config.getvalue(this.textbox1.text);
}
}
}
using system;
using system.configuration;
using system.reflection;
using system.web;
using system.xml;
public enum configfiletype
... {
webconfig,
appconfig
}
namespace configrw
... {
/**//// <summary>
/// summary description for readwriteconfig.
/// </summary>
public class readwriteconfig
...{
public string docname = string.empty;
private xmlnode node=null;
private int _configtype;
public string message;
public int configtype
...{
get...{ return _configtype; }
set...{ _configtype=value; }
}
getvalue#region getvalue
public string getvalue( string key )
...{ //读取
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
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +key +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
message = system.configuration.configurationsettings.appsettings[key];
return message;
}
catch
...{
message = "操作异常,获取value值失败!";
return message;
}
}
#endregion
setvalue#region setvalue
public string 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)
...{
message = "此key已经存在!";
return message;
}
// 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);
message = "添加成功!";
return message;
}
catch
...{
message = "操作异常,添加失败!";
return message;
}
}
#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 string 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");
}
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
// xpath select setting "add" element that contains this key to remove
node.removechild( node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") );
saveconfigdoc(cfgdoc,docname);
message = "删除成功!";
return message;
}
catch
...{
message = "操作异常,删除失败!";
return message;
}
}
#endregion
modifyelement#region modifyelement
public string modifyelement (string elementkey, string elementvalue)
...{ //修改
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
xmlelement addelem= (xmlelement)node.selectsinglenode("//add[@key=‘" +elementkey +"‘]") ;
if(addelem == null)
...{
message = "此key不存在!";
return message;
}
addelem.setattribute("value",elementvalue);
//save it
saveconfigdoc(cfgdoc,docname);
message = "修改成功!";
return message;
}
catch
...{
message = "操作异常,修改失败!";
return message;
}
}
#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
}
}
测试页面
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace configrw
... {
/**//// <summary>
/// summary description for webform1.
/// </summary>
public class webform1 : system.web.ui.page
...{
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.label label1;
protected system.web.ui.webcontrols.textbox textbox1;
protected system.web.ui.webcontrols.label label2;
protected system.web.ui.webcontrols.textbox textbox2;
protected system.web.ui.webcontrols.button button3;
protected system.web.ui.webcontrols.button button4;
protected system.web.ui.webcontrols.button button2;
private void page_load(object sender, system.eventargs e)
...{
}
web form designer generated code#region web form designer generated code
override protected void oninit(eventargs e)
...{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
/**//// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
...{
this.button1.click += new system.eventhandler(this.button1_click);
this.button2.click += new system.eventhandler(this.button2_click);
this.button3.click += new system.eventhandler(this.button3_click);
this.button4.click += new system.eventhandler(this.button4_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void button2_click(object sender, system.eventargs e)
...{
//新增
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.setvalue(this.textbox1.text,this.textbox2.text));
}
private void button1_click(object sender, system.eventargs e)
...{
//删除
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.removeelement(this.textbox1.text));
}
private void button3_click(object sender, system.eventargs e)
...{
//修改
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
response.write(config.modifyelement(this.textbox1.text,this.textbox2.text));
}
private void button4_click(object sender, system.eventargs e)
...{
//读取
readwriteconfig config = new readwriteconfig();
config.configtype = (int)configfiletype.webconfig;
this.textbox2.text = config.getvalue(this.textbox1.text);
}
}
}