In the ASP.NET configuration file Web.config, you can easily add a custom key value pair. As follows:
The value can be taken like this. String myvalue = system.configuration.configurationSettings.Appsettings ["mykey"]; 0000-00-00 0000-00-00 0000-00-00
However, the contents of web.config are changed to the entire httpApplication, and the frequently changed data in Web.config is not suitable.
Let me introduce a method, and use the XML format to save data, the file format is as follows: Password.config
xml version = "1.0" encoding = "gb2312"?>
Obviously, we can add any
String configfile = this.mappath ("password.config"); configSetting cs = configSetting.get setting; string password = cs ["name1"];
Look at this key class Using system; use system.io; use system.Web; use system.collection; using system.xml.Serialization; use system.xml.serialization;
Namespace cjl.Web.configuration {[namespace = "http://ajliang.126.com", Typename = "configuration"] public class configSetting {setting settings; public configSetting () {}
Public string this [string key] {get {return this.Settings [key];} set {this.settings [key] = value;}} [xmlelement (elementname = "settings")] public setting settings {get {ix settings == null) {settings = new Setting ();} return settings;} set {settings = value;}} public void SaveSetting (string xmlFile) {XmlSerializer serializer = new XmlSerializer (this.GetType ()); FileStream fs = New filestream (Xmlfile, Filemode.create); streamwriter sw = new streamwriter (fs, encoding.getencoding ("gb2312"); serializer.Serialize (SW, this); sw.close ();}
public static ConfigSetting GetSetting (string xmlFile) {HttpContext context = HttpContext.Current; object s = context.Cache [xmlFile]; if (s == null) {XmlSerializer serializer = new XmlSerializer (typeof (ConfigSetting)); FileStream fs = new FileStream (XMLFile, Filemode.Open); s = serializer.deSerialize (fs); fs.close (); context.cache.insert (XMLFile, s, new cachedependency (xmlfile);}
Return (configSetting) s;}
Public class setting {add [adds; [xmlelement (elementname = "add")] public add [] add {get {return adds;} set {adds = value;}}
Public string this [string key] {get {if (adds == null) {throw new exception ("no add element");} string str = null; foreach (add add in address {if (add.key == Key) {str = add.value; break;}} {throw new Exception ("There is no elemental add" property key = " " ");} return str;} set {i (add) {add a = new add (); a.key = key; a.Value = value; adds = new add [}}; return;} foreach (add aa in address) {ix Aa.Key == key) {aa.value = value; return;}} add aaa = new add (); aaa.key = key; aaa.value = value; add [] myadds = new add [add.length 1]; adds.copyto (myadds, 0); myadds [myadds.length - 1] = aaa; adds = myadds;}} // public string this [string str]} // public class setting
Public class add {[Xmlattribute (AttributeName = "key")] public string key; [xmlattribute (attributename = "value")] public string value;}
} //
} // namespace cjl.configuration
Save, modify, add demo code to refer to WebForm1.aspx
<% @ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "testconfigsetting.webform1"%>