获取web.config中appseting配置节信息

来源:互联网 发布:淘宝刷好评会被判刑吗 编辑:程序博客网 时间:2024/06/10 20:57
using System;
using System.Configuration;

    
/// <summary>
    
/// web.config操作类
    
/// </summary>

    public sealed class ConfigHelper
    
{
        
/// <summary>
        
/// 得到AppSettings中的配置字符串信息
        
/// </summary>
        
/// <param name="key"></param>
        
/// <returns></returns>

        public static string GetConfigString(string key)
        
{
            
return ConfigurationSettings.AppSettings[key];
        }

        
/// <summary>
        
/// 得到AppSettings中的配置bool信息
        
/// </summary>
        
/// <param name="key"></param>
        
/// <returns></returns>

        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;
        }

        
/// <summary>
        
/// 得到AppSettings中的配置decimal信息
        
/// </summary>
        
/// <param name="key"></param>
        
/// <returns></returns>

        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;
        }

        
/// <summary>
        
/// 得到AppSettings中的配置int信息
        
/// </summary>
        
/// <param name="key"></param>
        
/// <returns></returns>

        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;
        }

    }