APP.config/web.config编辑类
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
WebConfig,
AppConfig
}
namespace fenghua.Configuration
{
/// <summary>
/// AppConfig 的摘要说明。
/// </summary>
public class AppConfig : 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 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 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=HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load( docName );
return cfgDoc;
}
}
}
使用方法:
1.app.config
private void Form1_Load(object sender, System.EventArgs e)
{
AppConfig config = new AppConfig();
config.ConfigType= (int)ConfigFileType.AppConfig;
bool bln= (bool)( config.GetValue( "Boolean", typeof( bool ) ) );
string str= (string)( config.GetValue( "String", typeof( string ) ) );
DateTime date = (DateTime)( config.GetValue( "DateTime", typeof( DateTime ) ) );
bln = !bln;
str = str + "-" + "there";
date = date.AddDays( 1 );
config.SetValue( "Boolean", bln.ToString() );
config.SetValue( "String", str );
config.SetValue( "DateTime", date.ToShortDateString() );
config.SetValue("testing", "1234506");
config.SetValue("howdy", "there");
string str2= (string)( config.GetValue( "howdy", typeof( string ) ) );
textBox1.Text+= bln +" ";
textBox1.Text+= str2+" " ;
textBox1.Text+= date.ToShortDateString()+" " ;
// uncomment to see element removed
//config.removeElement("howdy");
}
2.修改web.config:
private void Page_Load(object sender, System.EventArgs e)
{
AppConfig config = new AppConfig();
//config.ConfigType = (int)ConfigFileType.WebConfig;
bool bln= (bool)( config.GetValue( "Boolean", typeof( bool ) ) );
string str= (string)( config.GetValue( "String", typeof( string ) ) );
DateTime date = (DateTime)( config.GetValue( "DateTime", typeof( DateTime ) ) );
bln = !bln;
str = str + "-" + "there";
date = date.AddDays( 1 );
config.SetValue( "Boolean", bln.ToString() );
config.SetValue( "String", str );
config.SetValue( "DateTime", date.ToShortDateString() );
config.SetValue("testing", "1234506");
config.SetValue("howdy", "there");
string str2= (string)( config.GetValue( "howdy", typeof( string ) ) );
Response.Write( bln + "<BR>" );
Response.Write( str2 + "<BR>");
Response.Write( date.ToShortDateString() + "<BR>" );
// uncomment to see element removed
//config.removeElement("howdy");
}