[ASP.NET] 數字與日期格式判斷 取得設定檔資料

摘要:[ASP.NET] 數字與日期格式判斷 取得設定檔資料

分享一下C#基本會用到的格式判斷與設定檔取得方法,提供給大家參考。

        /// 
        /// 判斷是否為數字
        /// 
        public static bool IsNumeric(object obj)
        {
            double retNum;
            bool Result = Double.TryParse(obj.ToString(), System.Globalization.NumberStyles.Any
                                , System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

            return Result;
        }
        /// 
        /// 判斷是否為日期
        /// 
        public static bool IsDate(object obj)
        {
            DateTime date;

            return DateTime.TryParse(obj.ToString(), out date);
        }
        /// 
        /// 取得連接字串
        /// 
        public static string getDBConnectionString(DBConnType ConnType)
        {
            string ConnectionString = string.Empty;
            ConnectionStringSettings Setting = ConfigurationManager.ConnectionStrings[ConnType.ToString()];

            if (Setting != null)
                ConnectionString = Setting.ConnectionString;

            return ConnectionString;
        }
        /// 
        /// 取得設定檔設定值
        /// 
        public static string getAPSetting(string key, int index = 0)
        {
            string Value = string.Empty;
            string[] aryValues = ConfigurationManager.AppSettings.GetValues(key);

            if (aryValues != null && aryValues.Length > index)
                Value = aryValues[index];
           
            return Value;
        }