日期:2014-05-16 浏览次数:20755 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Data; using System.Data.OleDb; using System.Text.RegularExpressions; using System.Configuration; using System.Web.UI; using System.Web.UI.WebControls; using System.Security.Cryptography; using System.Globalization; public class Unitl { #region 验证是否是数字 /// <summary> /// 验证是否是数字 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsNumeric(string str) { if (str == null || str.Length == 0) return false; System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); byte[] bytestr = ascii.GetBytes(str); foreach (byte c in bytestr) { if (c < 48 || c > 57) { return false; } } return true; } #endregion #region 生成随机数. static int iseed = 0; /// <summary> /// 生成数字和字母随机数. /// </summary> /// <param name="n">生成长度</param> /// <returns></returns> public static string RandCode(int nLeng) { char[] arrChar = new char[]{ '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z' , 'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z' }; StringBuilder num = new StringBuilder(); iseed++; iseed = iseed % 10000; Random rnd = new Random(iseed); for (int i = 0; i < nLeng; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); } /// <summary> /// 生成数字和字母随机数 . /// </summary> /// <param name="n">生成长度</param> /// <returns></returns> public static string RandCode(string RCstr, int nLeng) { StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < nLeng; i++) { num.Append(RCstr[rnd.Next(0, RCstr.Length)].ToString()); } return num.ToString(); } /// <summary> /// 生成按当前日期串组成的字符串. /// </summary> /// <param name="fileExtName"></param> /// <returns></returns> public static string RandFileName(string fileExtName) { return string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddhhmmss"), RandCode(8), fileExtName); } #endregion #region md5加密 /// <summary> /// 对字符串进行MD5加密. /// </summary> /// <param name="str"></param> /// <returns></returns> public static string Md5(string str) { return Md5(str, "utf-8"); } /// <summary> /// MD5加密. /// </summary> /// <param name="str"></param> /// <param name="charset"></param> /// <returns></returns> public static string Md5(string str, string charset) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(str)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString();