123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web.Security;
- using System.Security.Cryptography;
- using System.IO;
- using System.Web;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.Reflection;
- using System.Data;
- public static class DHelper
- {
- #region 常用处理
- /// <summary>
- /// 检查DataTable 是否有数据行
- /// </summary>
- /// <param name="dt">DataTable</param>
- /// <returns></returns>
- public static bool IsHaveRows(this System.Data.DataTable dt)
- {
- if (dt != null && dt.Rows.Count > 0)
- return true;
- return false;
- }
- /// <summary>
- /// 对象是否为NULL
- /// </summary>
- /// <param name="str">对象</param>
- /// <returns>是否为NULL</returns>
- public static bool IsNull(this object obj)
- {
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, DBNull.Value)) || (obj == null))
- {
- return true;
- }
- if (obj is DateTime && obj.ToDateTime().Equals(DateTime.MinValue))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 是否包含str字符有true
- /// </summary>
- /// <param name="strs"></param>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsIndexOf(this string strs, string str)
- {
- if (strs.ToLower().IndexOf(str.ToLower()) != -1)
- return true;
- return false;
- }
- /// <summary>
- /// 字符串是否为空true是空
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>是否为空</returns>
- public static bool IsEmpty(this string str)
- {
- if (IsNull(str))
- {
- return true;
- }
- if (str.Equals(String.Empty))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 对象不为NULL
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>返回字符串</returns>
- public static string IfNull(this object str)
- {
- if (str == null || Object.Equals(str, null))
- {
- return String.Empty;
- }
- return str.ToString();
- }
- /// <summary>
- /// 对象不为NULL
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>返回字符串</returns>
- public static bool IfNotNull(this object str)
- {
- if ((Object.Equals(str, null)) || (Object.Equals(str, DBNull.Value)) || (str == null))
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 有值?(与IsNullOrEmpty相反)
- /// </summary>
- /// <returns></returns>
- public static bool IsValuable(this IEnumerable<object> thisValue)
- {
- if (thisValue == null || thisValue.Count() == 0) return false;
- return true;
- }
- /// <summary>
- /// 字符串不为NULL
- /// </summary>
- /// <param name="str">要验证字符串</param>
- /// <param name="val">为空返回字符串</param>
- /// <returns>返回字符串</returns>
- public static string IfNull(this string str, string retStr)
- {
- if (str == null || Object.Equals(str, null))
- {
- return retStr;
- }
- return str;
- }
- /// <summary>
- /// 字符串数组转换成数字
- /// </summary>
- /// <param name="strs">字符串</param>
- /// <returns>数字</returns>
- public static int[] StrsToInts(this string[] strs)
- {
- int[] ints = new int[strs.Length];
- for (int i = 0; i < strs.Length; i++)
- {
- ints[i] = Convert.ToInt32(strs[i]);
- }
- return ints;
- }
- /// <summary>
- /// 取得自定义长度的字符串
- /// </summary>
- /// <param name="str">字符串</param>
- /// <param name="lenght">长度</param>
- /// <returns>截取后字符串</returns>
- public static string CutChar(this string str, int lenght)
- {
- if (str.Length > lenght)
- {
- return str.Substring(0, lenght) + DString.OMITTED;
- }
- else
- {
- return str;
- }
- }
- /// <summary>
- /// 取得自定义长度的字符串 中文123456789
- /// </summary>
- /// <param name="str">字符串</param>
- /// <param name="lenght">长度</param>
- /// <returns>截取后字符串</returns>
- public static string CutCNChar(this string str, int lenght)
- {
- string ret = String.Empty;
- if (str.Length > lenght)
- {
- int tempnum1 = 0;
- int tempnum2 = 0;
- byte[] byitem = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
- for (int i = 0; i < str.Length; i++)
- {
- if ((int)byitem[i] != 63)
- {
- tempnum1++;
- }
- else
- {
- tempnum2++;
- }
- if (tempnum2 * 2 + tempnum1 >= lenght * 2)
- {
- break;
- }
- }
- ret = str.Substring(0, tempnum2 + tempnum1) + DString.OMITTED;
- }
- else
- {
- ret = str;
- }
- return ret;
- }
- /// <summary>
- /// 返回字符串真实长度 1个汉字长度为2
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>实际字符串长度</returns>
- public static int GetCharLength(this string str)
- {
- return Encoding.Default.GetBytes(str).Length;
- }
- /// <summary>
- /// 检测是否有Sql危险字符
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeSqlString(this string str)
- {
- return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
- }
- public static bool IsSafety(this string str)
- {
- string text1 = Regex.Replace(str.Replace("%20", " "), @"\s", " ");
- string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or ";
- return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
- }
- /// <summary>
- /// 替换字符串
- /// </summary>
- /// <param name="Str">要替换的字符型</param>
- /// <returns>替换后的字符串</returns>
- public static string ReplaceW(this string str)
- {
- return Regex.Replace(str, DString.W, String.Empty).ToString();
- }
- /// <summary>
- /// SHA1加密字符串
- /// </summary>
- /// <param name="str">要加密的字符串</param>
- /// <returns>加密后的字符串</returns>
- public static string EncryptSHA1(this string str)
- {
- if (!str.IsEmpty())
- {
- return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.SHA1);
- }
- return str;
- }
- /// <summary>
- /// MD5加密字符串
- /// </summary>
- /// <param name="str">要加密的字符串</param>
- /// <returns>加密后的字符串</returns>
- public static string EncryptMD5(this string str)
- {
- if (!str.IsEmpty())
- {
- return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.MD5);
- }
- return str;
- }
- /// <summary>
- /// DES加密字符串
- /// </summary>
- /// <param name="encryptString">待加密的字符串</param>
- /// <param name="encryptKey">加密密钥,要求为8位</param>
- /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
- public static string EncryptDES(this string encryptString, string encryptKey)
- {
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
- byte[] rgbIV = DConfig.KEYS;
- byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
- DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Convert.ToBase64String(mStream.ToArray());
- }
- catch (Exception ex)
- {
- // Log.WriteLog(DMessage.ERR, ex);
- }
- return encryptString;
- }
- /// <summary>
- /// DES解密字符串
- /// </summary>
- /// <param name="decryptString">待解密的字符串</param>
- /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
- /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
- public static string DecryptDES(this string decryptString, string decryptKey)
- {
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
- byte[] rgbIV = DConfig.KEYS;
- byte[] inputByteArray = Convert.FromBase64String(decryptString);
- DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Encoding.UTF8.GetString(mStream.ToArray());
- }
- catch (Exception ex)
- {
- ////DLog.WriteLog(DMessage.ERR, ex);
- }
- return decryptString;
- }
- /// <summary>
- /// 生成随机字符串
- /// </summary>
- /// <param name="length">字符串长度</param>
- /// <returns>随机字符串</returns>
- public static string RandChar(int length)
- {
- string strSep = DString.COMMA;
- char[] chrSep = strSep.ToCharArray();
- //因1与l不容易分清楚,所以去除
- //因0与O不容易分清楚,所以去除
- string strChar = DString.CHAR;
- string[] aryChar = strChar.Split(chrSep, strChar.Length);
- string strRandom = string.Empty;
- Random Rnd = new Random();
- //生成随机字符串
- for (int i = 0; i < length; i++)
- {
- strRandom += aryChar[Rnd.Next(aryChar.Length)];
- }
- return strRandom;
- }
- /// <summary>
- /// 生成随机数字字符串
- /// </summary>
- /// <param name="length">字符串长度</param>
- /// <returns>随机字符串</returns>
- public static string RandNum(int length)
- {
- string strSep = DString.COMMA;
- char[] chrSep = strSep.ToCharArray();
- //因1与l不容易分清楚,所以去除
- //因0与O不容易分清楚,所以去除
- string strChar = DString.NUM;
- string[] aryChar = strChar.Split(chrSep, strChar.Length);
- string strRandom = string.Empty;
- Random rnd = new Random();
- //生成随机字符串
- for (int i = 0; i < length; i++)
- {
- strRandom += aryChar[rnd.Next(aryChar.Length)];
- }
- return strRandom;
- }
- /// <summary>
- /// 生成随机汉子字符串
- /// </summary>
- /// <param name="length">字符串长度</param>
- /// <returns>随机字符串</returns>
- public static string RandCN(int length)
- {
- string strSep = DString.COMMA;
- char[] chrSep = strSep.ToCharArray();
- string strChar = DString.CN;
- //定义一个字符串数组储存汉字编码的组成元素
- string[] rBase = strChar.Split(chrSep, strChar.Length);
- Random rnd = new Random();
- //定义一个object数组用来
- object[] bytes = new object[length];
- /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
- 每个汉字有四个区位码组成
- 区位码第1位和区位码第2位作为字节数组第一个元素
- 区位码第3位和区位码第4位作为字节数组第二个元素
- */
- for (int i = 0; i < length; i++)
- {
- //区位码第1位
- int r1 = rnd.Next(11, 14);
- string str_r1 = rBase[r1].Trim();
- //区位码第2位
- rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
- int r2;
- if (r1 == 13)
- {
- r2 = rnd.Next(0, 7);
- }
- else
- {
- r2 = rnd.Next(0, 16);
- }
- string str_r2 = rBase[r2].Trim();
- //区位码第3位
- rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
- int r3 = rnd.Next(10, 16);
- string str_r3 = rBase[r3].Trim();
- //区位码第4位
- rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
- int r4;
- if (r3 == 10)
- {
- r4 = rnd.Next(1, 16);
- }
- else if (r3 == 15)
- {
- r4 = rnd.Next(0, 15);
- }
- else
- {
- r4 = rnd.Next(0, 16);
- }
- string str_r4 = rBase[r4].Trim();
- //定义两个字节变量存储产生的随机汉字区位码
- byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
- byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
- //将两个字节变量存储在字节数组中
- byte[] str_r = new byte[] { byte1, byte2 };
- //将产生的一个汉字的字节数组放入object数组中
- bytes.SetValue(str_r, i);
- }
- Encoding gb = Encoding.GetEncoding(DString.GBK);
- string[] str = new string[length];
- string chars = String.Empty;
- for (int i = 0; i < length; i++)
- {
- str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
- chars = chars + str[i];
- }
- return chars;
- }
- /// <summary>
- /// 生成随机文件名
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <returns>随机文件名</returns>
- public static string RandFileName()
- {
- return DObject.DATE_TIME + RandChar(5);
- }
- /// <summary>
- /// 生成随机订单号
- /// </summary>
- /// <returns>随机文件名</returns>
- public static string RandOrder()
- {
- return DObject.DATE_TIME + RandNum(8);
- }
- /// <summary>
- /// 生成小文件名
- /// </summary>
- /// <returns>原文件名</returns>
- public static string GetMin(this string filename)
- {
- return filename.Substring(0, filename.LastIndexOf(DString.POINT)) + DString.MIN + filename.Substring(filename.LastIndexOf(DString.POINT), filename.Length - filename.LastIndexOf(DString.POINT));
- }
- /// <summary>
- /// 生成缩略图
- /// </summary>
- /// <param name="originalImagePath">源图路径(物理路径)</param>
- /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
- /// <param name="width">缩略图宽度</param>
- /// <param name="height">缩略图高度</param>
- /// <param name="mode">生成缩略图的方式 3:指定高宽缩放(可能变形)2:指定宽,高按比例 1:指定高,宽按比例0:指定高宽裁减(不变形)</param>
- public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, int mode)
- {
- bool isSuccess = true;
- Image originalImage = Image.FromFile(originalImagePath);
- int towidth = width;
- int toheight = height;
- int x = 0;
- int y = 0;
- int ow = originalImage.Width;
- int oh = originalImage.Height;
- switch (mode)
- {
- case 3://指定高宽缩放(可能变形)
- break;
- case 2://指定宽,高按比例
- toheight = originalImage.Height * width / originalImage.Width;
- break;
- case 1://指定高,宽按比例
- towidth = originalImage.Width * height / originalImage.Height;
- break;
- case 0://指定高宽裁减(不变形)
- if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
- {
- oh = originalImage.Height;
- ow = originalImage.Height * towidth / toheight;
- y = 0;
- x = (originalImage.Width - ow) / 2;
- }
- else
- {
- ow = originalImage.Width;
- oh = originalImage.Width * height / towidth;
- x = 0;
- y = (originalImage.Height - oh) / 2;
- }
- break;
- default:
- break;
- }
- //新建一个bmp图片
- Image bitmap = new Bitmap(towidth, toheight);
- //新建一个画板
- Graphics g = Graphics.FromImage(bitmap);
- //设置高质量插值法
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode = SmoothingMode.HighQuality;
- //清空画布并以透明背景色填充
- g.Clear(Color.White);
- //在指定位置并且按指定大小绘制原图片的指定部分
- g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
- new Rectangle(x, y, ow, oh),
- GraphicsUnit.Pixel);
- try
- {
- //以jpg格式保存缩略图
- bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
- }
- catch (System.Exception ex)
- {
- isSuccess = false;
- ////DLog.WriteLog(DMessage.ERR, ex);
- }
- finally
- {
- originalImage.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- return isSuccess;
- }
- /// <summary>
- /// 根据属性名提取属性值
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="obj">要提取的对象</param>
- /// <param name="name">属性名</param>
- /// <returns>提取出的属性值</returns>
- public static object GetPropertyByName<T>(T obj, string name)
- {
- try
- {
- Type t = obj.GetType();
- PropertyInfo pi = t.GetProperty(name);
- return pi.GetValue(obj, null);
- }
- catch (System.Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return new Object();
- }
- /// <summary>
- /// 根据属性名设置值
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="obj">要设置的对象</param>
- /// <param name="name">属性名</param>
- /// <param name="val">属性值</param>
- /// <returns>设置值后的对象</returns>
- public static T SetPropertyByName<T>(T obj, string name, object val)
- {
- try
- {
- Type t = obj.GetType();
- PropertyInfo pi = t.GetProperty(name);
- pi.SetValue(obj, val, null);
- }
- catch (System.Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return obj;
- }
- #endregion
- #region 网页
- #region 常用
- /// <summary>
- /// 提取网址
- /// </summary>
- /// <param name="url">地址</param>
- /// <returns>真实地址</returns>
- public static string GetResolvedUrl(this string url)
- {
- return DObject.CURRECT_PAGE.ResolveUrl(url);
- }
- #endregion
- #region 页面跳转
- /// <summary>
- /// 页面出错
- /// </summary>
- public static void GoError()
- {
- DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_ERROR);
- }
- /// <summary>
- /// 无访问权限
- /// </summary>
- public static void GoNoPower()
- {
- DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOACCESS);
- }
- /// <summary>
- /// 找不到页面
- /// </summary>
- public static void GoNoFound()
- {
- DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOFOUND);
- }
- /// <summary>
- /// 网站维护中
- /// </summary>
- public static void GoMaintenance()
- {
- DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_MAINTENANCE);
- }
- #endregion
- #region COOKIE
- /// <summary>
- /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires
- /// </summary>
- /// <param name="strCookieName">COOKIE对象名</param>
- /// <param name="iExpires">COOKIE对象有效时间(秒数),0一周内有效,1表示永久有效,大于等于2表示具体有效秒数</param>
- /// <param name="strValue">COOKIE对象Value值</param>
- public static void SetCookie(string strCookieName, int iExpires, string strValue)
- {
- HttpCookie objCookie = new HttpCookie(strCookieName);
- objCookie.Value = UrlEncode(strValue.Trim());
- if (iExpires >= 0)
- {
- if (iExpires.Equals(0))
- {
- objCookie.Expires = DateTime.Now.AddDays(7);
- }
- else if (iExpires.Equals(1))
- {
- objCookie.Expires = DateTime.MaxValue;
- }
- else
- {
- objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
- }
- }
- DObject.CURRECT_PAGE.Response.Cookies.Add(objCookie);
- }
- /// <summary>
- /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回字符串"CookieNonexistence"
- /// </summary>
- /// <param name="strCookieName">Cookie对象名称</param>
- /// <returns>Value值,如果对象本就不存在,则返回字符串""</returns>
- public static string GetCookie(string strCookieName)
- {
- if (DObject.CURRECT_PAGE.Request.Cookies[strCookieName].IsNull())
- {
- return String.Empty;
- }
- else
- {
- return UrlDecode(DObject.CURRECT_PAGE.Request.Cookies[strCookieName].Value);
- }
- }
- /// <summary>
- /// URL解码
- /// </summary>
- /// <param name="str">要解码的字符串</param>
- /// <returns>解码后的字符串</returns>
- public static string UrlDecode(this string str)
- {
- try
- {
- return HttpUtility.UrlDecode(str);
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return str;
- }
- /// <summary>
- /// URL编码
- /// </summary>
- /// <param name="str">要编码的字符串</param>
- /// <returns>编码后的字符串</returns>
- public static string UrlEncode(this string str)
- {
- try
- {
- return HttpUtility.UrlEncode(str);
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return str;
- }
- /// <summary>
- /// HTML解码
- /// </summary>
- /// <param name="str">要解码的字符串</param>
- /// <returns>解码后的字符串</returns>
- public static string HtmlDecode(this string str)
- {
- try
- {
- return HttpUtility.HtmlDecode(str);
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return str;
- }
- /// <summary>
- /// HTML编码
- /// </summary>
- /// <param name="str">要编码的字符串</param>
- /// <returns>编码后的字符串</returns>
- public static string HtmlEncode(this string str)
- {
- try
- {
- return HttpUtility.HtmlEncode(str);
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return str;
- }
- #endregion
- #region Session
- /// <summary>
- /// 读取session值
- /// </summary>
- /// <param name="name">名称</param>
- /// <returns>值</returns>
- public static object GetSession(string name)
- {
- object Str_Value = null;
- try
- {
- Str_Value = DObject.CURRECT_PAGE.Session[name];
- }
- catch (Exception ex)
- {
- Str_Value = null;
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return Str_Value;
- }
- /// <summary>
- /// 读取session值
- /// </summary>
- /// <param name="name">名称</param>
- /// <returns>值</returns>
- public static string GetStrSession(string name)
- {
- string Str_Value = null;
- try
- {
- Str_Value = DObject.CURRECT_PAGE.Session[name].ToString();
- }
- catch (Exception ex)
- {
- Str_Value = String.Empty;
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return Str_Value;
- }
- /// <summary>
- /// 设置session值
- /// </summary>
- /// <param name="name">名称</param>
- /// <param name="pObj_Value">值</param>
- public static void SetSession(string name, object value)
- {
- try
- {
- DObject.CURRECT_PAGE.Session[name] = value;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- }
- #endregion
- #region HTML
- /// <summary>
- /// 读取提交值
- /// </summary>
- /// <param name="name">名称</param>
- /// <returns>提交值</returns>
- public static string GetQueryString(string name)
- {
- if (DObject.CURRECT_PAGE.Request[name].IsNull())
- {
- return String.Empty;
- }
- return DObject.CURRECT_PAGE.Request[name];
- }
- /// <summary>
- /// 取得当前URL
- /// </summary>
- /// <returns>当前URL</returns>
- public static string GetPath()
- {
- string strPath = String.Format(DString.STRING_URL,
- HttpContext.Current.Request.ServerVariables[DString.STRING_HOST],
- HttpContext.Current.Request.ServerVariables[DString.STRING_PATH_INFO],
- HttpContext.Current.Request.ServerVariables[DString.STRING_QUERY_STRING]);
- if (strPath.IndexOf(DString.QM) > 0)
- {
- strPath = strPath.Substring(0, strPath.IndexOf(DString.QM));
- }
- return strPath;
- }
- /// <summary>
- /// Aspx 转换到 Html
- /// </summary>
- /// <param name="url">地址</param>
- /// <returns>字符串</returns>
- public static string AspxToHtml(this string url)
- {
- //判断路径是否为空
- if (!url.IsEmpty())
- {
- //分割路径
- string[] temp = url.Split(DString.CHAR_QM);
- //路径不合法
- if (temp.Length != 1 && temp.Length != 2)
- {
- return url;
- }
- //获取路径后缀
- string ext = Path.GetExtension(temp[0]);
- //后缀不为ASPX直接返回
- if (!(ext.Equals(DString.STRING_ASPX, StringComparison.OrdinalIgnoreCase)))
- {
- return url;
- }
- //截取.aspx中前面的内容
- int offset = temp[0].LastIndexOf(DString.CHAR_POINT);
- string resource = temp[0].Substring(0, offset);
- //路径不带参数时
- if (temp.Length == 1 || string.IsNullOrEmpty(temp[1]))
- {
- return string.Format(DString.STRING_HTML0, resource);
- }
- string aurl = temp[1].Replace(DString.CHAR_EQ, DString.CHAR_UNDERLINE);
- //路径带参数时
- return string.Format(DString.STRING_HTML01, resource, aurl.Replace(DString.AMP, DString.UNDERLINE_T));
- }
- return String.Empty;
- }
- /// <summary>
- /// Html 转换到 Aspx
- /// </summary>
- /// <param name="url">地址</param>
- /// <returns>字符串</returns>
- public static string HtmlToAspx(this string url)
- {
- //判断路径是否为空
- if (!url.IsEmpty())
- {
- string ext = Path.GetExtension(url);
- //路径不为HTML
- if (!(ext.Equals(DString.STRING_HTML, StringComparison.OrdinalIgnoreCase)))
- {
- return url;
- }
- string[] temp = url.Split(new String[] { DString.UNDERLINE_TH, DString.POINT }, StringSplitOptions.RemoveEmptyEntries);
- if (temp.Length == 2)
- {
- return string.Format(DString.STRING_ASPX0, temp[0]);
- }
- if (temp.Length == 3)
- {
- string aurl = temp[1].Replace(DString.UNDERLINE_T, DString.AMP);
- return String.Format(DString.STRING_ASPX01, temp[0], aurl.Replace(DString.CHAR_UNDERLINE, DString.CHAR_EQ));
- }
- }
- return String.Empty;
- }
- /// <summary>
- /// 过滤HTML代码
- /// </summary>
- /// <param name="fieldValue">要过滤的数据</param>
- /// <returns>过滤后的数据</returns>
- public static string FilterHTML(this string str)
- {
- Regex regexScript = new Regex(DString.STRING_SCRIPT, RegexOptions.IgnoreCase);
- Regex regexHref = new Regex(DString.STRING_HREFSCRIPT, RegexOptions.IgnoreCase);
- Regex regexOn = new Regex(DString.HTML_EVEN, RegexOptions.IgnoreCase);
- Regex regexIframe = new Regex(DString.STRING_IFRAME, RegexOptions.IgnoreCase);
- Regex regexFrameset = new Regex(DString.HTML_IFRAMESET, RegexOptions.IgnoreCase);
- string html = str.Trim();
- html = regexScript.Replace(html, String.Empty); //过滤<script></script>标记
- html = regexHref.Replace(html, String.Empty); //过滤href=javascript: (<A>) 属性
- html = regexOn.Replace(html, DString.HTML_EVEN_R); //过滤其它控件的on事件
- html = regexIframe.Replace(html, String.Empty); //过滤iframe
- html = regexFrameset.Replace(html, String.Empty); //过滤frameset
- return html;
- }
- #endregion
- #endregion
- #region 类型转换
- /// <summary>
- /// 转换成INT
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static int ToInt32(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- int ret = Convert.ToInt32(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return 0;
- }
- /// <summary>
- /// 转换成DataTable
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static DataTable ToDataTable(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return new DataTable();
- }
- DataTable ret = obj as DataTable;
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return new DataTable();
- }
- /// <summary>
- /// 转换成List集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static List<T> ToModList<T>(this object obj) where T : new()
- {
- try
- {
- if (obj.IsNull())
- {
- return new List<T>();
- }
- List<T> ret = obj as List<T>;
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return new List<T>();
- }
- /// <summary>
- /// 转换成List集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static T ToMod<T>(this object obj) where T : new()
- {
- try
- {
- if (obj.IsNull())
- {
- return new T();
- }
- T ret = (T)obj;
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return new T();
- }
- /// <summary>
- /// 转换成INT
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static int ToInt32(this object obj, int defValue)
- {
- try
- {
- if (obj.IsNull())
- {
- return defValue;
- }
- int ret = Convert.ToInt32(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return defValue;
- }
- /// <summary>
- /// 转换成INT
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static decimal ToDec(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- decimal ret = Convert.ToDecimal(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return 0;
- }
- /// <summary>
- /// 转换成INT
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static decimal ToDec(this object obj, decimal defValue)
- {
- try
- {
- if (obj.IsNull())
- {
- return defValue;
- }
- decimal ret = Convert.ToDecimal(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return defValue;
- }
- /// <summary>
- /// 转换成BOOL
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static double ToDou(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return 0;
- }
- double ret = Convert.ToDouble(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return 0;
- }
- public static float ToFloat(this object obj, int defValue)
- {
- try
- {
- if (obj.IsNull())
- {
- return defValue;
- }
- float ret = Convert.ToInt32(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return defValue;
- }
- /// <summary>
- /// 转换成BOOL
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static double ToDou(this object obj, double defValue)
- {
- try
- {
- if (obj.IsNull())
- {
- return defValue;
- }
- double ret = Convert.ToDouble(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return defValue;
- }
- /// <summary>
- /// 转换成BOOL
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static bool ToBool(this object obj)
- {
- try
- {
- if (obj.IsNull())
- {
- return false;
- }
- bool ret = Convert.ToBoolean(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return false;
- }
- /// <summary>
- /// 转换成BOOL
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换结果</returns>
- public static DateTime ToDateTime(this object obj)
- {
- try
- {
- DateTime ret = Convert.ToDateTime(obj);
- return ret;
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return DObject.EMPTY_DATE_TIME;
- }
- public static string ToDateTimeStr(this DateTime obj)
- {
- try
- {
- return obj.ToString("yyyy-MM-dd");
- }
- catch (Exception ex)
- {
- //DLog.WriteLog(DMessage.ERR, ex);
- }
- return "";
- }
- #endregion
- }
|