C # Custom Read configuration file class

Source: Internet
Author: User
This C # class defines a common way to read appsettings configuration files, which makes it easy to read field information from the appsettings configuration file for string, number, and bool types. Login, register, subscribe, RSS, sitemap
Using system;using system.configuration; Namespace dotnet.utilities{//// Web. config operation class //public sealed class ConfigHelper {///// get configuration string information in appsettings /////////
 
  
 public static string getconfigstring (String key) {string CacheKey = "appsettings-" + key;            Object Objmodel = Datacache.getcache (CacheKey); if (Objmodel = = null) {try {Objmodel = ConfigurationManager.                    Appsettings[key]; if (Objmodel! = null) {Datacache.setcache (CacheKey, OBJM                    Odel, DateTime.Now.AddMinutes (n.), TimeSpan.Zero);        }} catch {}} return objmodel.tostring (); }          ////// Get configuration bool information in appsettings /////////
 
  
 public static bool Getconfigbool (string key) {bool result = false;            String cfgval = getconfigstring (key); if (null! = Cfgval && string. Empty! = Cfgval) {try {result = bool.                Parse (Cfgval);                } catch (FormatException) {//Ignore format exceptions.        }} return result; }        ////// Get configuration decimal information in the appsettings ////////
 
  
 public static decimal Getconfigdecimal (string key) {decimal result = 0;            String cfgval = getconfigstring (key); if (null! = Cfgval && string. Empty! = Cfgval) {try {result = decimal.                Parse (Cfgval);                } catch (FormatException) {//Ignore format exceptions.        }} return result; }        ////// get configuration int information in appsettings /////////
 
  
 public static int Getconfigint (string key) {int result = 0;            String cfgval = getconfigstring (key); if (null! = Cfgval && string. Empty! = Cfgval) {try {result = Int.                Parse (Cfgval);                } catch (FormatException) {//Ignore format exceptions.        }} return result; }    }}
  • Related Article

    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.