The data of the AppSettings node in the web.config configuration file can be easily accessed by the AppSettings attribute of the ConfigurationSettings class in System.Configuration. For web programs, some read-only program information is stored, such as program name, author information, database connection string, etc. will be very convenient. Such as: private void page_load (object sender, system.eventargs e) {this.tbname = configurationSettings.appsettings ["appname"];
For the ConfigurationSettings class, there is a method GetConfig (String SectionName) to access any configuration elements, for the above example, can be used:
<-! Sample.aspx -> private void Page_Load (object sender, System.EventArgs e) {object settings = ConfigurationSettings.GetConfig ( "appSettings"); NameValueCollection nvc = settings as NameValueCollection; if (nvc = null!) { String Val = (string) NVC ["AppName"]; this.tbname = Val;}}
It can be seen that the getConfig () method returns a configuration processing object, and after converting an instance of the NameValueCollection, you can access the content within the section. In fact, for the configuration file retrieved, there is a handler implementation, and we can see that in Web.config, or Machine.Config, see the declaration of the handler, such as:
<-! Web.config ->
<-! Settings.cs -> using Chagel.Configration.Data; namespace Chagel.Configration {public class Settings: IConfigurationSectionHandler {// Create method to achieve the public object interface Create (object parent, object input, XmlNode node) { Data Data = New Data (); Foreach (XMLNode Xn in node.childNodes) {switch (xn.name) {case ("appname"): data.appname = xn.innerText; Break; Case ("appver): DATA .Appver = xn.innertext; ...} // switch end} // foreach end
Return Data;
} //Method end}}
The iConfigurationSectionHandler interface has only one way. Whenever you find the configuration section registered to the handler, you will call the create method on the section handler. We implemented the class returns an instance of a DATA class. This class is a specialized dataset, code as follows:
namespace chagel.configration.data {public class data {public data () {} public string appname; // Program Name public string appver; // Program PUBLIC STRING AppAuthor; // Author ...}}
At this point, you can now read the configuration element value, such as:
Private Void Page_Load (Object Sender, System.EventArgs E) {data Data; Data = ConfigurationSettings.getconfig ("Mysection") AS DATA; this.tbname.text = data.appname;}
By implementing a class supports the IconFigurationSectionHandler interface to complete the readings of the custom festival. Of course, we can still declare the system's handler (system.configuration.namevaluefileesectionhandler) reusing the same class as AppSettings.