The site has a lot to set the content, such as site information, registration settings, upload settings. If saving in a database requires a separate table, there is only one record in the table, which makes the database bloated and the efficiency of frequent access to the database a problem. and save in config file is a good choice, and with caching function!
We can write configuration in the Web.config configuration section.
<configuration>
<configSections>
<section name= "EntityFramework" type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=6.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirepermission= false"/>
<!--Here you can define configuration sections-->
<!--for more Information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468-->
</configSections>
But putting a lot of configuration in here can also cause Web.config to be bloated, with the configuration section defined here and the specific configuration information stored in other files.
Take the upload configuration information for example, look at the ideal structure
The content of Config\upload.config
1, the configuration element. Using the form of <add/>, is a key and a worthy representation. <add key= "MaxSize" value= "1500000"/>.
2, the set of elements, which defines a series of elements. <UploadConfig>......</UploadConfig>
3, a custom section that represents a custom node. <section name= "Uploadconfig" type= "Ninesky.Models.Config.UploadConfig, Ninesky.models"/>
Name: node name, type: The class row that handles the node, before the comma is the class name, and after the comma is indented in the assembly.
We want to create this class to manage the configuration
First, create the key, the value element class.
There are only two read and write properties key and value, the content is very simple
Using System.Configuration;
Namespace Ninesky.Models.Config
{
///<summary>
///Key-value element class
///<remarks>
/// Created: 2014.03.09
///</remarks>
///</summary> public
class Keyvalueelement: ConfigurationElement
{
///<summary>
///key
///</summary>
[ ConfigurationProperty ("Key", Isrequired=true)] public
string Key {get
{return this["key"]. ToString ();
set {this["key"] = value;}
}
<summary>
///value
///</summary>
[ConfigurationProperty ("value")] public
string Value
{Get
{return this[' value ']. ToString ();
set {this[' value '] = value;}}
}
Second, create the element collection class. the content is simple and is commented on
Using System;
Using System.Configuration; namespace Ninesky.Models.Config {///<summary>///element Collection class///<remarks>///creation: 2014.03.09///</remark s>///</summary> [Configurationcollection (typeof (Keyvalueelement))] public class Keyvalueelementcollection : configurationelementcollection {//Ignore case public keyvalueelementcollection (): Base (stringcomparer.ordinalignorecase {}///<summary>///collection elements of the specified key///</summary>///<param name= ' name ' ></param>/// ;returns></returns> new public keyvalueelement this[string name] {get {return (keyvalueelement) base. Baseget (name); The set {if (base.
Properties.contains (name)) Base[name] = value; else base.
Baseadd (value);
}///<summary>///create a new element. Must rewrite///</summary>///<returns></returns> protected override ConfigurationElement Createnewe
Lement () {return new keyvalueelement (); }///<summary>///get the key of the element///</summary>///<param name= "element" ></param>///<returns></return S> protected Override Object Getelementkey (configurationelement Element) {return ((keyvalueelement) Element).
Key;
}
}
}
Third, the configuration section class
Class to define the private property keyvalues. Read and write a collection of configuration sections, and then define a series of required configuration attributes, which are read and write to keyvalues elements.
Using System.Configuration; namespace Ninesky.Models.Config {///<summary>///upload Settings configuration section///<remarks>///Create: 2014.03.09///</rema Rks>///</summary> public class Uploadconfig:configurationsection {private static ConfigurationProperty _p Roperty = new ConfigurationProperty (string.
Empty, typeof (Keyvalueelementcollection), NULL, configurationpropertyoptions.isdefaultcollection); <summary>///Configuration list///</summary> [ConfigurationProperty ("", Options = configurationpropertyoptions. Isdefaultcollection)] Private keyvalueelementcollection KeyValues {get {return (keyvalueelementcollection) base[_p Roperty];
set {Base[_property] = value;}
///<summary>///Maximum size///</summary> public int MaxSize {get {int _value = 0; if (keyvalues["MaxSize"]!= null) int. TryParse (keyvalues["MaxSize").
Value, out _value);
return _value; set {if (keyvalues["MaxSize"] = = null) keyvalues["MaxSize"] = new Keyvalueelement () {Key = "MaxSize", value = value.
ToString ()}; else keyvalues["MaxSize"]. Value = value.
ToString ();
}///<summary>///upload directory///folder name for application directory, front without ~////</summary> public string Path {get
{if (keyvalues["Path"] = = null) return "Upload"; return keyvalues["Path".
Value; set {if (keyvalues["path"] = = null) keyvalues["path" = new Keyvalueelement () {Key = "path", value = value}
; else keyvalues["Path".
Value = value;
}///<summary>///allows upload of picture type file extensions, multiple with "," separated///</summary> public string Imageext {get { if (keyvalues["imageext"] = = null) return string.
Empty; return keyvalues["Imageext"].
Value; set {if (keyvalues["imageext"] = = null) keyvalues["Imageext" = new Keyvalueelement () {Key = "Imageext", Val
UE = value}; else keyvalues["Imageext"].
Value = value; }///<summary>///allows the upload of flash type file extensions, multiple "," separated
</summary> public string Flashext {get {if (keyvalues["flashext"] = = null) return string.
Empty; return keyvalues["Flashext"].
Value; set {if (keyvalues["flashext"] = = null) keyvalues["Flashext" = new Keyvalueelement () {Key = "Flashext", Val
UE = value}; else keyvalues["Flashext"].
Value = value;
}///<summary>///allows uploading of media type file extensions, multiple with "," separated///</summary> public string Mediaext {get { if (keyvalues["mediaext"] = = null) return string.
Empty; return keyvalues["Mediaext"].
Value; set {if (keyvalues["mediaext"] = = null) keyvalues["Mediaext" = new Keyvalueelement () {Key = "Mediaext", Val
UE = value}; else keyvalues["Mediaext"].
Value = value;
}///<summary>///allow uploaded file type file extensions, multiple "," separated///</summary> public string Fileext {get { if (keyvalues["fileext"] = = null) return string.
Empty; return keyvalues["Fileext"].
Value;
} set { if (keyvalues["fileext"] = = null) keyvalues["Fileext"] = new Keyvalueelement () {Key = "Fileext", value = value}; else keyvalues["Fileext"].
Value = value;
}
}
}
}
Four, read the configuration
is also the most related, the configuration information how to read out. It's easy to read after writing in the above class, just use Webconfigurationmanager.openwebconfiguration ("~"). GetSection () Gets an instance of the configuration section, which can use the property value
var _uploadconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("~"). GetSection ("Uploadconfig") as Ninesky.Models.Config.UploadConfig;
File max limit
int _maxsize = _uploadconfig.maxsize;
File path
string _fileparth = _uploadconfig.path;
string _dirname;
The type allowed to be uploaded
Hashtable exttable = new Hashtable ();
Exttable.add ("image", _uploadconfig.imageext);
Exttable.add ("Flash", _uploadconfig.fileext);
Exttable.add ("Media", _uploadconfig.mediaext);
Exttable.add ("file", _uploadconfig.fileext);
V. Write configuration
similar to read, set the property value to save (), and the content will be saved to the config\upload.config.
The code is as follows:
var _uploadconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("~"). GetSection ("Uploadconfig") as Ninesky.Models.Config.UploadConfig;
File max limit
int _maxsize = _uploadconfig.maxsize;
_uploadconfig.fileext = "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2";
_uploadconfig.flashext = "swf,flv";
_uploadconfig.imageext = "Gif,jpg,jpeg,png,bmp";
_uploadconfig.mediaext = "SWF,FLV,MP3,WAV,WMA,WMV,MID,AVI,MPG,ASF,RM,RMVB";
_uploadconfig.path = "Upload";
_uploadconfig.currentconfiguration.save ();
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.