Read the configuration information class in the Web. config file

Source: Internet
Author: User

Using System;
Using System. Web;
Using System. Collections. Generic;
Using System. Configuration;
Using System. Linq;
Using System. Xml;

Namespace CommHelper
{
Public class AppConfig
{
/// <Summary>
/// Obtain the bool value of the Configuration Attribute
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> [True/False] </returns>
Public static bool GetBool (string key)
{
Bool isBool = false;
Try
{
IsBool = Convert. ToBoolean (ConfigurationManager. deleetask[ key]);
}
Catch {
}
Return isBool;
}
/// <Summary>
/// Obtain the Int value of the Configuration Attribute
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static int GetInt (string key)
{
Int iValue =-1;
Try
{
IValue = Convert. ToInt32 (ConfigurationManager. etettings [key]);
}
Catch {}
Return iValue;
}
/// <Summary>
/// Obtain the string value of the Configuration Attribute
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string GetString (string key)
{
Return Convert. ToString (ConfigurationManager. deleetask[ key]);
}
/// <Summary>
/// Obtain the Int value of the Configuration Attribute
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "name"> </param>
/// <Returns> </returns>
Public static int GetInt (string key, string name)
{
String str = GetString (key, name );
If (str! = Null)
{
Return Convert. ToInt32 (str );
}
Return-99999;
}
/// <Summary>
/// Obtain the string value of the Configuration Attribute
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "name"> </param>
/// <Returns> </returns>
Public static string GetString (string key, string name)
{
XmlDocument document = new XmlDocument ();
Document. Load (HttpContext. Current. Server. MapPath ("/web. config "));
XmlElement element = (XmlElement) document. SelectSingleNode (string. Format ("/configuration/extendSettings/add [@ key = '{0}']", key ));
If (element! = Null)
{
Return element. GetAttribute (name );
}
Return null;
}

[Obsolete ("This method is obsolete, it has been replaced by GetString method.", true)]
Public static string ReadAppSetting (string key)
{
Return ConfigurationManager. deleetask[ key];
}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.