haoxiaobo

从C到C++又到.net, 有一些心得, 和大家交流下...
随笔 - 64, 文章 - 6, 评论 - 635, 阅读 - 18万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

取得程序设定的常用代码.

Posted on   HAL9000  阅读(754)  评论(0编辑  收藏  举报

using System;
using System.Configuration;

namespace HXBTools.Util
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    public class Config
    
{
        
public Config()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public static string GetSettingString(string sKeyName, string sDefault)
        
{
            
string s = ConfigurationSettings.AppSettings[sKeyName];
            
if (s == null)
            
{
                
return sDefault;
            }

            
else
            
{
                
return s;
            }

        }


        
public static int GetSettingInt(string sKeyName, int iDefault)
        
{
            
string s = Config.GetSettingString(sKeyName, null);
            
if (s == null)
                
return iDefault;

            
int i;
            
try
            
{
                i 
= int.Parse(s);
                
return i;
            }

            
catch
            
{
                
return iDefault;
            }


        }



        
public static long GetSettingLong(string sKeyName, long lDefault)
        
{
            
string s = Config.GetSettingString(sKeyName, null);
            
if (s == null)
                
return lDefault;

            
long l;
            
try
            
{
                l 
= long.Parse(s);
                
return l;
            }

            
catch
            
{
                
return lDefault;
            }


        }



        
public static decimal GetSettingDecimal(string sKeyName, decimal dcDefault)
        
{
            
string s = Config.GetSettingString(sKeyName, null);
            
if (s == null)
                
return dcDefault;

            
decimal dc;
            
try
            
{
                dc 
= decimal.Parse(s);
                
return dc;
            }

            
catch
            
{
                
return dcDefault;
            }


        }


        
        
public static DateTime GetSettingDateTime(string sKeyName, DateTime dtDefaule)
        
{
            
string s = Config.GetSettingString(sKeyName, null);
            
if (s == null)
                
return dtDefaule;

            DateTime dt;
            
try
            
{
                dt 
= DateTime.Parse(s);
                
return dt;
            }

            
catch
            
{
                
return dtDefaule;
            }


        }


        
public static bool GetSettingBool(string sKeyName, bool bDefault)
        
{
            
string s = Config.GetSettingString(sKeyName, null);
            
if (s == null)
                
return bDefault;
            
            
switch(s.Trim().ToLower())
            
{
                
case "true":
                
case "":
                
case "":
                
case "1":
                
case "yes":
                
case "ok":
                
case "-1":
                    
return true;
                
case "false":
                
case "0":
                
case "":
                
case "":
                
case "":
                
case "":
                
case "no":
                
case "not":
                
case "never":
                    
return false;
                
default:                    
                    
return bDefault;
            }

        }
        
    }

}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示