using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Net; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Web; namespace HZQR.Common { /// /// 帮助类 /// public class Utils { #region 系统版本 /// /// 版本信息类 /// public class VersionInfo { public int FileMajorPart { get { return 3; } } public int FileMinorPart { get { return 0; } } public int FileBuildPart { get { return 0; } } public string ProductName { get { return "HiiShe.Cms"; } } public int ProductType { get { return 0; } } } //public static string GetVersion() //{ // return ConfigerKeys.ASSEMBLY_VERSION; //} #endregion #region MD5加密 public static string MD5(string pwd) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = System.Text.Encoding.Default.GetBytes(pwd); byte[] md5data = md5.ComputeHash(data); md5.Clear(); string str = ""; for (int i = 0; i < md5data.Length; i++) { str += md5data[i].ToString("x").PadLeft(2, '0'); } return str; } #endregion #region 判断对象类型(Int32、double、email、url),数据类型转换 #region 判断对象是否为Int32类型的数字 /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(object expression) { if (expression != null) return IsNumeric(expression.ToString()); return false; } /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(string expression) { if (expression != null) { string str = expression; if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) { if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) return true; } } return false; } #endregion #region 是否为Double类型 /// /// 是否为Double类型 /// /// /// public static bool IsDouble(object expression) { if (expression != null) return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); return false; } #endregion #region 检测是否为手机号码 public static bool IsPhoneNumber(string PhoneNumber) { /* 中国电信号段 * 133、149、153、173、177、180、181、189、190、191、193、199 * 中国联通号段 * 130、131、132、145、155、156、166、167、171、175、176、185、186、196 * 中国移动号段 * 134(0-8)、135、136、137、138、139、1440、147、148、150、151、152、157、158、159、 * 172、178、182、183、184、187、188、195、197、198*/ string rightphone = @"^(1(3|4|5|6|7|8|9)[0-9])\d{8}$"; if (PhoneNumber.Length != 11) { return false; } Regex regex = new Regex(rightphone); return regex.IsMatch(PhoneNumber); } #endregion #region 检测是否符合email格式 /// /// 检测是否符合email格式 /// /// 要判断的email字符串 /// 判断结果 public static bool IsValidEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]"); } public static bool IsValidDoEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } #endregion #region 检测是否是正确的Url /// /// 检测是否是正确的Url /// /// 要验证的Url /// 判断结果 public static bool IsURL(string strUrl) { return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$"); } #endregion #region 将字符串转换为数组 /// /// 将字符串转换为数组 /// /// 字符串 /// 字符串数组 public static string[] GetStrArray(string str) { return str.Split(new char[',']); } #endregion #region 将字符串转换为list /// /// 将字符串转换为list /// /// 字符串 /// 字符串数组 public static List GetStrList(string str) { List list = new List(); foreach(string s in str.Split(',')) { list.Add(s); } return list; } #endregion #region 将数组转换为字符串 /// /// 将数组转换为字符串 /// /// 字符串数组 /// 分隔符 /// String public static string GetArrayStr(string[] ArrayStr, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ArrayStr.Length; i++) { if (i == ArrayStr.Length - 1) { sb.Append(ArrayStr[i]); } else { sb.Append(ArrayStr[i]); sb.Append(speater); } } return sb.ToString(); } #endregion #region 将List转换为字符串 /// /// 将List转换为字符串 /// /// 字符串List /// 分隔符 /// String public static string GetArrayStr(List stringlist, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < stringlist.Count; i++) { if (i == stringlist.Count - 1) { sb.Append(stringlist[i]); } else { sb.Append(stringlist[i]); sb.Append(speater); } } return sb.ToString(); } #endregion #region object型转换为bool型 /// /// object型转换为bool型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的bool类型结果 public static bool StrToBool(object expression, bool defValue) { if (expression != null) return StrToBool(expression, defValue); return defValue; } #endregion #region string型转换为bool型 /// /// string型转换为bool型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的bool类型结果 public static bool StrToBool(string expression, bool defValue) { if (expression != null) { if (string.Compare(expression, "true", true) == 0) return true; else if (string.Compare(expression, "false", true) == 0) return false; } return defValue; } #endregion #region 将对象转换为Int32类型 /// /// 将对象转换为Int32类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static int ObjToInt(object expression, int defValue) { if (expression != null) return StrToInt(expression.ToString(), defValue); return defValue; } #endregion #region 将字符串转换为Int32类型 /// /// 将字符串转换为Int32类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static int StrToInt(string expression, int defValue) { if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")) return defValue; int rv; if (Int32.TryParse(expression, out rv)) return rv; return Convert.ToInt32(StrToFloat(expression, defValue)); } #endregion #region Object型转换为decimal型 /// /// Object型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static decimal ObjToDecimal(object expression, decimal defValue) { if (expression != null) return StrToDecimal(expression.ToString(), defValue); return defValue; } #endregion #region string型转换为decimal型 /// /// string型转换为decimal型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的decimal类型结果 public static decimal StrToDecimal(string expression, decimal defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; decimal intValue = defValue; if (expression != null) { bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsDecimal) decimal.TryParse(expression, out intValue); } return intValue; } #endregion #region Object型转换为float型 /// /// Object型转换为float型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static float ObjToFloat(object expression, float defValue) { if (expression != null) return StrToFloat(expression.ToString(), defValue); return defValue; } #endregion #region string型转换为float型 /// /// string型转换为float型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static float StrToFloat(string expression, float defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; float intValue = defValue; if (expression != null) { bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsFloat) float.TryParse(expression, out intValue); } return intValue; } #endregion #region 将对象转换为日期时间类型 /// /// 将对象转换为日期时间类型 /// /// 要转换的对象 /// 缺省值 /// 转换后的DateTime类型结果 public static DateTime ObjectToDateTime(object obj, DateTime defValue) { return StrToDateTime(obj.ToString(), defValue); } /// /// 将对象转换为日期时间类型 /// /// 要转换的对象 /// 转换后的DateTime类型结果 public static DateTime ObjectToDateTime(object obj) { return StrToDateTime(obj.ToString()); } /// /// 将对象转换为日期时间类型 /// /// 要转换的字符串 /// 转换后的DateTime类型结果 public static DateTime StrToDateTime(string str) { return StrToDateTime(str, DateTime.Now); } /// /// 将对象转换为日期时间类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static DateTime StrToDateTime(string str, DateTime defValue) { if (!string.IsNullOrEmpty(str)) { DateTime dateTime; if (DateTime.TryParse(str, out dateTime)) return dateTime; } return defValue; } #endregion #region 将对象转换为字符串 /// /// 将对象转换为字符串 /// /// 要转换的对象 /// 转换后的string类型结果 public static string ObjectToStr(object obj) { if (obj == null) return ""; return obj.ToString().Trim(); } #endregion #endregion #region 分割字符串 /// /// 分割字符串 /// public static string[] SplitString(string strContent, string strSplit) { if (!string.IsNullOrEmpty(strContent)) { if (strContent.IndexOf(strSplit) < 0) return new string[] { strContent }; return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase); } else return new string[0] { }; } /// /// 分割字符串 /// /// public static string[] SplitString(string strContent, string strSplit, int count) { string[] result = new string[count]; string[] splited = SplitString(strContent, strSplit); for (int i = 0; i < count; i++) { if (i < splited.Length) result[i] = splited[i]; else result[i] = string.Empty; } return result; } #endregion #region 删除最后结尾的一个逗号 /// /// 删除最后结尾的一个逗号 /// public static string DelLastComma(string str) { if (str.Length < 1) { return ""; } return str.Substring(0, str.LastIndexOf(",")); } #endregion #region 删除最后结尾的指定字符后的字符 /// /// 删除最后结尾的指定字符后的字符 /// public static string DelLastChar(string str, string strchar) { if (string.IsNullOrEmpty(str)) return ""; if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1) { return str.Substring(0, str.LastIndexOf(strchar)); } return str; } #endregion #region 生成指定长度的字符串 /// /// 生成指定长度的字符串,即生成strLong个str字符串 /// /// 生成的长度 /// 以str生成字符串 /// public static string StringOfChar(int strLong, string str) { string ReturnStr = ""; for (int i = 0; i < strLong; i++) { ReturnStr += str; } return ReturnStr; } #endregion #region 生成日期随机码 /// /// 生成日期随机码 /// /// public static string GetRamCode() { #region return DateTime.Now.ToString("yyyyMMddHHmmssffff"); #endregion } #endregion #region 生成随机字母或数字 /// /// 生成随机数字 /// /// 生成长度 /// public static string Number(int Length) { return Number(Length, false); } /// /// 生成随机数字 /// /// 生成长度 /// 是否要在生成前将当前线程阻止以避免重复 /// public static string Number(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); string result = ""; System.Random random = new Random(); for (int i = 0; i < Length; i++) { result += random.Next(10).ToString(); } return result; } /// /// 生成随机字母字符串(数字字母混和) /// /// 待生成的位数 public static string GetCheckCode(int codeCount) { string str = string.Empty; int rep = 0; long num2 = DateTime.Now.Ticks + rep; rep++; Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep))); for (int i = 0; i < codeCount; i++) { char ch; int num = random.Next(); if ((num % 2) == 0) { ch = (char)(0x30 + ((ushort)(num % 10))); } else { ch = (char)(0x41 + ((ushort)(num % 0x1a))); } str = str + ch.ToString(); } return str; } /// /// 根据日期和随机码生成订单号 /// /// public static string GetOrderNumber() { string num = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms return num + Number(2).ToString(); } private static int Next(int numSeeds, int length) { byte[] buffer = new byte[length]; System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider(); Gen.GetBytes(buffer); uint randomResult = 0x0;//这里用uint作为生成的随机数 for (int i = 0; i < length; i++) { randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8)); } return (int)(randomResult % numSeeds); } #endregion #region 截取字符长度 /// /// 截取字符长度 /// /// 字符 /// 长度 /// public static string CutString(string inputString, int len) { if (string.IsNullOrEmpty(inputString)) return ""; inputString = DropHTML(inputString); ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (mybyte.Length > len) tempString += "…"; return tempString; } #endregion #region 清除HTML标记 public static string DropHTML(string Htmlstring) { if (string.IsNullOrEmpty(Htmlstring)) return ""; //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"]*?>.*?", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"