using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Web; namespace RoadFlow.Utility { public class Tools { [Obsolete("请用RoadFlow.Utility.DateTimeNew.Now", true)] public static DateTime DateTime { get { return DateTimeNew.Now; } } public static System.IO.MemoryStream GetValidateImg(out string code, string bgImg = "WorkFlow/Images/vcodebg.png") { code = GetValidateCode(); Random rnd = new Random(); System.Drawing.Bitmap img = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 17.2)), 28); System.Drawing.Image bg = System.Drawing.Bitmap.FromFile(HttpContext.Current.Server.MapPath(bgImg)); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img); System.Drawing.Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic)); System.Drawing.Font fontbg = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true); g.DrawImage(bg, 0, 0, new System.Drawing.Rectangle(rnd.Next(bg.Width - img.Width), rnd.Next(bg.Height - img.Height), img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); g.DrawString(code, fontbg, System.Drawing.Brushes.White, 0, 1); g.DrawString(code, font, System.Drawing.Brushes.Green, 0, 1);//字颜色 //画图片的背景噪音线 int x = img.Width; int y1 = rnd.Next(5, img.Height); int y2 = rnd.Next(5, img.Height); g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Green, 2), 1, y1, x - 2, y2); g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Transparent), 0, 10, img.Width - 1, img.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return ms; } private static string GetValidateCode() { //产生五位的随机字符串 int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 4; i++) { number = random.Next(); if (number % 2 == 0) code = (char)('0' + (char)(number % 10)); else if (number % 3 == 0) code = (char)('a' + (char)(number % 26)); else code = (char)('A' + (char)(number % 26)); checkCode += code == '0' || code == 'O' ? "x" : code.ToString(); } return checkCode; } /// /// 获取远程浏览器端 IP 地址 /// /// 返回 IPv4 地址 public static string GetIPAddress() { string userHostAddress = HttpContext.Current.Request.UserHostAddress; if (userHostAddress.IsNullOrEmpty()) { userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } return userHostAddress; } /// /// 得到用户浏览器类型 /// /// public static string GetBrowse() { return System.Web.HttpContext.Current.Request.Browser.Type; } /// /// 获取浏览器端操作系统名称 /// /// public static string GetOSName() { string osVersion = System.Web.HttpContext.Current.Request.Browser.Platform; string userAgent = System.Web.HttpContext.Current.Request.UserAgent; if (userAgent.Contains("NT 6.3")) { osVersion = "Windows8.1"; } else if (userAgent.Contains("NT 6.2")) { osVersion = "Windows8"; } else if (userAgent.Contains("NT 6.1")) { osVersion = "Windows7"; } else if (userAgent.Contains("NT 6.0")) { osVersion = "WindowsVista"; } else if (userAgent.Contains("NT 5.2")) { osVersion = "WindowsServer2003"; } else if (userAgent.Contains("NT 5.1")) { osVersion = "WindowsXP"; } else if (userAgent.Contains("NT 5")) { osVersion = "Windows2000"; } else if (userAgent.Contains("NT 4")) { osVersion = "WindowsNT4.0"; } else if (userAgent.Contains("Me")) { osVersion = "WindowsMe"; } else if (userAgent.Contains("98")) { osVersion = "Windows98"; } else if (userAgent.Contains("95")) { osVersion = "Windows95"; } else if (userAgent.Contains("Mac")) { osVersion = "Mac"; } else if (userAgent.Contains("Unix")) { osVersion = "UNIX"; } else if (userAgent.Contains("Linux")) { osVersion = "Linux"; } else if (userAgent.Contains("SunOS")) { osVersion = "SunOS"; } return osVersion; } /// /// 得到分页HTML /// /// 记录总数 /// 每页条数 /// 当前页 /// 查询字符串 /// public static string GetPagerHtml(long recordCount, int pageSize, int pageNumber, string queryString) { //得到共有多少页 long PageCount = recordCount <= 0 ? 1 : recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1; long pNumber = pageNumber; if (pNumber < 1) { pNumber = 1; } else if (pNumber > PageCount) { pNumber = PageCount; } //如果只有一页则返回空分页字符串 if (PageCount <= 1) { return ""; } StringBuilder ReturnPagerString = new StringBuilder(1500); string JsFunctionName = string.Empty; //构造分页字符串 int DisplaySize = 10;//中间显示的页数 ReturnPagerString.Append("
"); ReturnPagerString.Append("共 " + recordCount.ToString() + " 条 每页 条 "); ReturnPagerString.Append("转到 "); if (pNumber > 1) ReturnPagerString.Append("«"); //添加第一页 if (pNumber >= DisplaySize / 2 + 3) ReturnPagerString.Append("1…"); else ReturnPagerString.Append("1"); //添加中间数字 long star = pNumber - DisplaySize / 2; long end = pNumber + DisplaySize / 2; if (star < 2) { end += 2 - star; star = 2; } if (end > PageCount - 1) { star -= end - (PageCount - 1); end = PageCount - 1; } if (star < 2) star = 2; for (long i = star; i <= end; i++) ReturnPagerString.Append("" + i.ToString() + ""); //添加最后页 if (pNumber <= PageCount - (DisplaySize / 2)) ReturnPagerString.Append("…" + PageCount.ToString() + ""); else if (PageCount > 1) ReturnPagerString.Append("" + PageCount.ToString() + ""); if (pNumber < PageCount) ReturnPagerString.Append("»"); ReturnPagerString.Append("
"); //构造分页JS函数 ReturnPagerString.Append(""); return ReturnPagerString.ToString(); } /// /// 得到页尺寸 /// /// public static int GetPageSize() { string size = System.Web.HttpContext.Current.Request["pagesize"] ?? "15"; int size1; return size.IsInt(out size1) ? size1 : 15; } /// /// 得到页号 /// /// public static int GetPageNumber() { string number = System.Web.HttpContext.Current.Request["pagenumber"] ?? "1"; int number1; return number.IsInt(out number1) ? number1 : 1; } /// /// 得到列表项 /// /// 列表, 标题,值 /// 默认值 /// 是不显示空选项 /// 空选项显示标题 /// public static System.Web.UI.WebControls.ListItem[] GetListItems(IList list, string value, bool showEmpty = false, string emptyTitle = "") { List items = new List(); if (showEmpty) { items.Add(new System.Web.UI.WebControls.ListItem(emptyTitle, "")); } foreach (var li in list) { if (li.Length < 2) { continue; } var item = new System.Web.UI.WebControls.ListItem(li[0], li[1]); item.Selected = !value.IsNullOrEmpty() && value == li[1] && !items.Exists(p => p.Selected); items.Add(item); } return items.ToArray(); } public static System.Web.UI.WebControls.ListItem[] GetListItems(IList list, string value, bool showEmpty = false, string emptyTitle = "") { List newList = new List(); foreach (string str in list) { newList.Add(new string[] { str, str }); } return GetListItems(newList, value, showEmpty, emptyTitle); } /// /// 将服务器控件列表项转换为select列表项 /// /// /// public static string GetOptionsString(System.Web.UI.WebControls.ListItem[] items) { StringBuilder options = new StringBuilder(items.Length * 50); foreach (var item in items) { options.AppendFormat(""); } return options.ToString(); } /// /// 将服务器控件列表项转换为Checkbox项 /// /// /// public static string GetCheckBoxString(System.Web.UI.WebControls.ListItem[] items, string name, string[] values, string otherAttr = "") { StringBuilder options = new StringBuilder(items.Length * 50); foreach (var item in items) { string tempid = Guid.NewGuid().ToString("N"); options.AppendFormat("", item.Value.Replace("\"", "'"), values != null && values.Contains(item.Value) ? "checked=\"checked\"" : "", string.Format("{0}_{1}", name, tempid), name, otherAttr ); options.AppendFormat(""); } return options.ToString(); } /// /// 将服务器控件列表项转换为Radio项 /// /// /// public static string GetRadioString(System.Web.UI.WebControls.ListItem[] items, string name, string otherAttr = "") { StringBuilder options = new StringBuilder(items.Length * 50); foreach (var item in items) { string tempid = Guid.NewGuid().ToString("N"); options.AppendFormat("", item.Value.Replace("\"", "'"), item.Selected ? "checked=\"checked\"" : "", string.Format("{0}_{1}", name, tempid), name, otherAttr ); options.AppendFormat(""); } return options.ToString(); } /// /// 得到是否选择项 /// /// /// /// /// public static System.Web.UI.WebControls.ListItem[] GetYesNoListItems(string value, bool showEmpty = false, string emptyString = "") { List list = new List(); list.Add(new string[] { "是", "1" }); list.Add(new string[] { "否", "0" }); return GetListItems(list, value, showEmpty, emptyString); } /// /// 得到sql语句in里的字符串 /// /// 字符串 /// public static string GetSqlInString(string str, bool isSingleQuotes = true, string split = ",") { string[] strArray = str.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries); return GetSqlInString(strArray, isSingleQuotes); } /// /// 得到sql语句in里的字符串 /// /// /// /// 是否加单引号,字符串要加,数字不加 /// public static string GetSqlInString(T[] strArray, bool isSingleQuotes = true) { StringBuilder inStr = new StringBuilder(strArray.Length * 40); foreach (var s in strArray) { if (s.ToString().IsNullOrEmpty()) { continue; } if (isSingleQuotes) { inStr.Append("'"); } inStr.Append(s.ToString().Trim()); if (isSingleQuotes) { inStr.Append("'"); } inStr.Append(","); } return inStr.ToString().TrimEnd(','); } /// /// 产生不重复随机数 /// /// 共产生多少随机数 /// 最小值 /// 最大值 /// int[]数组 public static int[] GetRandomNum(int count, int minValue, int maxValue) { Random rnd = new Random(Guid.NewGuid().GetHashCode()); int length = maxValue - minValue + 1; byte[] keys = new byte[length]; rnd.NextBytes(keys); int[] items = new int[length]; for (int i = 0; i < length; i++) { items[i] = i + minValue; } Array.Sort(keys, items); int[] result = new int[count]; Array.Copy(items, result, count); return result; } /// /// 产生随机字符串 /// /// 字符串位数 public static string GetRandomString(int length = 5) { int number; char code; string checkCode = String.Empty; System.Random random = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < length + 1; i++) { number = random.Next(); if (number % 2 == 0) code = (char)('0' + (char)(number % 10)); else code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } return checkCode; } /// /// 产生随机字母 /// /// 字符串位数 public static string GetRandomLetter(int length = 2) { int number; char code; string checkCode = String.Empty; System.Random random = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < length; i++) { number = random.Next(); code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } return checkCode; } /// /// 得到一个文件的大小(单位KB) /// /// public static string GetFileSize(string file) { if (!System.IO.File.Exists(file)) { return ""; } System.IO.FileInfo fi = new System.IO.FileInfo(file); return (fi.Length/1000).ToString("###,###"); } public static string DataTableToJsonString(System.Data.DataTable dt) { LitJson.JsonData json = new LitJson.JsonData(); foreach(System.Data.DataRow dr in dt.Rows) { LitJson.JsonData drJson = new LitJson.JsonData(); for (int i = 0; i < dt.Columns.Count; i++) { LitJson.JsonData drJson1 = new LitJson.JsonData(); drJson1 = dr[i].ToString(); drJson.Add(drJson1); } json.Add(drJson); } return json.ToJson(); } } }