Advertisement
klimentmichal

app config values

May 19th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. namespace Configuration
  4. {
  5.    public static class SiteConfigurationReader
  6.    {
  7.       public static String appKeyString  //for string type value
  8.       {
  9.          get
  10.          {
  11.             return ConfigurationManager.AppSettings.Get("appKeyString");
  12.          }
  13.       }
  14.  
  15.       public static Int32 appKeyInt  //to get integer value
  16.       {
  17.          get
  18.          {
  19.             return ConfigurationManager.AppSettings.Get("appKeyInt").ToInteger(true);
  20.          }
  21.       }
  22.  
  23.       // you can also get the app setting by passing the key
  24.       public static Int32 GetAppSettingsInteger(string keyName)
  25.       {
  26.           try
  27.           {
  28.             return Convert.ToInt32(ConfigurationManager.AppSettings.Get(keyName));
  29.         }
  30.         catch
  31.         {
  32.             return 0;
  33.         }
  34.       }
  35.    }
  36. }
  37.  
  38.  
  39.  
  40.  
  41. <configuration>
  42.    <configSections>
  43.      <!-- some stuff omitted here -->
  44.    </configSections>
  45.    <appSettings>
  46.       <add key="appKeyString" value="abc" />
  47.       <add key="appKeyInt" value="123" />  
  48.    </appSettings>
  49. </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement