| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Configuration;
- namespace CallCenterApi.Common
- {
- public class Utils
- {
- #region 对象转换处理
- /// <summary>
- /// 判断对象是否为Int32类型的数字
- /// </summary>
- /// <param name="Expression"></param>
- /// <returns></returns>
- public static bool IsNumeric(object expression)
- {
- if (expression != null)
- return IsNumeric(expression.ToString());
- return false;
- }
- /// <summary>
- /// 判断对象是否为Int32类型的数字
- /// </summary>
- /// <param name="Expression"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 是否为Double类型
- /// </summary>
- /// <param name="expression"></param>
- /// <returns></returns>
- public static bool IsDouble(object expression)
- {
- if (expression != null)
- return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
- return false;
- }
- /// <summary>
- /// 将字符串转换为数组
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>字符串数组</returns>
- public static string[] GetStrArray(string str)
- {
- return str.Split(new char[',']);
- }
- /// <summary>
- /// 将数组转换为字符串
- /// </summary>
- /// <param name="list">List</param>
- /// <param name="speater">分隔符</param>
- /// <returns>String</returns>
- public static string GetArrayStr(List<string> list, string speater)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < list.Count; i++)
- {
- if (i == list.Count - 1)
- {
- sb.Append(list[i]);
- }
- else
- {
- sb.Append(list[i]);
- sb.Append(speater);
- }
- }
- return sb.ToString();
- }
- /// <summary>
- /// 字符串数组转字符串List
- /// </summary>
- /// <param name="strArray"></param>
- /// <returns></returns>
- public static List<string> StrArrayToList(string[] strArray)
- {
- List<string> list = new List<string>();
- foreach (string s in strArray)
- {
- list.Add(s);
- }
- return list;
- }
- /// <summary>
- /// object型转换为bool型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- public static bool StrToBool(object expression, bool defValue)
- {
- if (expression != null)
- return StrToBool(expression, defValue);
- return defValue;
- }
- /// <summary>
- /// string型转换为bool型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- 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;
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int ObjToInt(object expression, int defValue)
- {
- if (expression != null)
- return StrToInt(expression.ToString(), defValue);
- return defValue;
- }
- /// <summary>
- /// 将字符串转换为Int32类型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- 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));
- }
- /// <summary>
- /// Object型转换为decimal型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的decimal类型结果</returns>
- public static decimal ObjToDecimal(object expression, decimal defValue)
- {
- if (expression != null)
- return StrToDecimal(expression.ToString(), defValue);
- return defValue;
- }
- /// <summary>
- /// string型转换为decimal型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的decimal类型结果</returns>
- 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;
- }
- /// <summary>
- /// Object型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float ObjToFloat(object expression, float defValue)
- {
- if (expression != null)
- return StrToFloat(expression.ToString(), defValue);
- return defValue;
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- 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;
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime StrToDateTime(string str, DateTime defValue)
- {
- if (!string.IsNullOrEmpty(str))
- {
- DateTime dateTime;
- if (DateTime.TryParse(str, out dateTime))
- return dateTime;
- }
- return defValue;
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime StrToDateTime(string str)
- {
- return StrToDateTime(str, DateTime.Now);
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime ObjectToDateTime(object obj)
- {
- return StrToDateTime(obj.ToString());
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime ObjectToDateTime(object obj, DateTime defValue)
- {
- return StrToDateTime(obj.ToString(), defValue);
- }
- /// <summary>
- /// 将对象转换为字符串
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换后的string类型结果</returns>
- public static string ObjectToStr(object obj)
- {
- if (obj == null)
- return "";
- return obj.ToString().Trim();
- }
- #endregion
- #region 分割字符串
- /// <summary>
- /// 分割字符串
- /// </summary>
- 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] { };
- }
- /// <summary>
- /// 分割字符串
- /// </summary>
- /// <returns></returns>
- 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 删除最后结尾的一个逗号
- /// <summary>
- /// 删除最后结尾的一个逗号
- /// </summary>
- public static string DelLastComma(string str)
- {
- return str.Substring(0, str.LastIndexOf(","));
- }
- #endregion
- #region 删除最后结尾的指定字符后的字符
- /// <summary>
- /// 删除最后结尾的指定字符后的字符
- /// </summary>
- 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 生成指定长度的字符串
- /// <summary>
- /// 生成指定长度的字符串,即生成strLong个str字符串
- /// </summary>
- /// <param name="strLong">生成的长度</param>
- /// <param name="str">以str生成字符串</param>
- /// <returns></returns>
- public static string StringOfChar(int strLong, string str)
- {
- string ReturnStr = "";
- for (int i = 0; i < strLong; i++)
- {
- ReturnStr += str;
- }
- return ReturnStr;
- }
- #endregion
- #region 生成日期随机码
- /// <summary>
- /// 生成日期随机码
- /// </summary>
- /// <returns></returns>
- public static string GetRamCode()
- {
- #region
- return DateTime.Now.ToString("yyyyMMddHHmmssffff");
- #endregion
- }
- #endregion
- #region 生成随机字母或数字
- /// <summary>
- /// 生成随机数字
- /// </summary>
- /// <param name="length">生成长度</param>
- /// <returns></returns>
- public static string Number(int Length)
- {
- return Number(Length, false);
- }
- /// <summary>
- /// 生成随机数字
- /// </summary>
- /// <param name="Length">生成长度</param>
- /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 生成随机字母字符串(数字字母混和)
- /// </summary>
- /// <param name="codeCount">待生成的位数</param>
- 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;
- }
- /// <summary>
- /// 根据日期和随机码生成订单号
- /// </summary>
- /// <returns></returns>
- public static string GetOrderNumber()
- {
- string num = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssfff
- 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 截取字符长度
- /// <summary>
- /// 截取字符长度
- /// </summary>
- /// <param name="inputString">字符</param>
- /// <param name="len">长度</param>
- /// <returns></returns>
- 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, @"<script[^>]*?>.*?</script>", "", 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, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- Htmlstring.Replace("<", "");
- Htmlstring.Replace(">", "");
- Htmlstring.Replace("\r\n", "");
- Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
- return Htmlstring;
- }
- #endregion
- #region 清除HTML标记且返回相应的长度
- public static string DropHTML(string Htmlstring, int strLen)
- {
- return CutString(DropHTML(Htmlstring), strLen);
- }
- #endregion
- #region TXT代码转换成HTML格式
- /// <summary>
- /// 字符串字符处理
- /// </summary>
- /// <param name="chr">等待处理的字符串</param>
- /// <returns>处理后的字符串</returns>
- /// //把TXT代码转换成HTML格式
- public static String ToHtml(string Input)
- {
- StringBuilder sb = new StringBuilder(Input);
- sb.Replace("&", "&");
- sb.Replace("<", "<");
- sb.Replace(">", ">");
- sb.Replace("\r\n", "<br />");
- sb.Replace("\n", "<br />");
- sb.Replace("\t", " ");
- //sb.Replace(" ", " ");
- return sb.ToString();
- }
- #endregion
- #region HTML代码转换成TXT格式
- /// <summary>
- /// 字符串字符处理
- /// </summary>
- /// <param name="chr">等待处理的字符串</param>
- /// <returns>处理后的字符串</returns>
- /// //把HTML代码转换成TXT格式
- public static String ToTxt(String Input)
- {
- StringBuilder sb = new StringBuilder(Input);
- sb.Replace(" ", " ");
- sb.Replace("<br>", "\r\n");
- sb.Replace("<br>", "\n");
- sb.Replace("<br />", "\n");
- sb.Replace("<br />", "\r\n");
- sb.Replace("<", "<");
- sb.Replace(">", ">");
- sb.Replace("&", "&");
- return sb.ToString();
- }
- #endregion
- #region 检测是否有Sql危险字符
- /// <summary>
- /// 检测是否有Sql危险字符
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeSqlString(string str)
- {
- return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
- }
- /// <summary>
- /// 检查危险字符
- /// </summary>
- /// <param name="Input"></param>
- /// <returns></returns>
- public static string Filter(string sInput)
- {
- if (sInput == null || sInput == "")
- return null;
- string sInput1 = sInput.ToLower();
- string output = sInput;
- string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
- if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
- {
- throw new Exception("字符串中含有非法字符!");
- }
- else
- {
- output = output.Replace("'", "''");
- }
- return output;
- }
- /// <summary>
- /// 检查过滤设定的危险字符
- /// </summary>
- /// <param name="InText">要过滤的字符串 </param>
- /// <returns>如果参数存在不安全字符,则返回true </returns>
- public static bool SqlFilter(string word, string InText)
- {
- if (InText == null)
- return false;
- foreach (string i in word.Split('|'))
- {
- if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
- {
- return true;
- }
- }
- return false;
- }
- #endregion
- #region 过滤特殊字符
- /// <summary>
- /// 过滤特殊字符
- /// </summary>
- /// <param name="Input"></param>
- /// <returns></returns>
- public static string Htmls(string Input)
- {
- if (Input != string.Empty && Input != null)
- {
- string ihtml = Input.ToLower();
- ihtml = ihtml.Replace("<script", "<script");
- ihtml = ihtml.Replace("script>", "script>");
- ihtml = ihtml.Replace("<%", "<%");
- ihtml = ihtml.Replace("%>", "%>");
- ihtml = ihtml.Replace("<$", "<$");
- ihtml = ihtml.Replace("$>", "$>");
- return ihtml;
- }
- else
- {
- return string.Empty;
- }
- }
- #endregion
- #region 检查是否为IP地址
- /// <summary>
- /// 是否为ip
- /// </summary>
- /// <param name="ip"></param>
- /// <returns></returns>
- public static bool IsIP(string ip)
- {
- return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
- }
- #endregion
- #region 获得配置文件节点XML文件的绝对路径
- public static string GetXmlMapPath(string xmlName)
- {
- return "";
- //return GetMapPath(ConfigurationManager.AppSettings[xmlName].ToString());
- }
- #endregion
- #region 获得当前绝对路径
- /// <summary>
- /// 获得当前绝对路径
- /// </summary>
- /// <param name="strPath">指定的路径</param>
- /// <returns>绝对路径</returns>
- public static string GetMapPath(string strPath)
- {
- if (strPath.ToLower().StartsWith("http://"))
- {
- return strPath;
- }
- if (HttpContext.Current != null)
- {
- return HttpContext.Current.Server.MapPath(strPath);
- }
- else //非web程序引用
- {
- strPath = strPath.Replace("/", "\\");
- if (strPath.StartsWith("\\"))
- {
- strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
- }
- return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
- }
- }
- #endregion
- #region 文件操作
- /// <summary>
- /// 删除单个文件
- /// </summary>
- /// <param name="_filepath">文件相对路径</param>
- public static bool DeleteFile(string _filepath)
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- return false;
- }
- string fullpath = GetMapPath(_filepath);
- if (File.Exists(fullpath))
- {
- File.Delete(fullpath);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 删除上传的文件(及缩略图)
- /// </summary>
- /// <param name="_filepath"></param>
- public static void DeleteUpFile(string _filepath)
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- return;
- }
- string fullpath = GetMapPath(_filepath); //原图
- if (File.Exists(fullpath))
- {
- File.Delete(fullpath);
- }
- if (_filepath.LastIndexOf("/") >= 0)
- {
- string thumbnailpath = _filepath.Substring(0, _filepath.LastIndexOf("/")) + "mall_" + _filepath.Substring(_filepath.LastIndexOf("/") + 1);
- string fullTPATH = GetMapPath(thumbnailpath); //宿略图
- if (File.Exists(fullTPATH))
- {
- File.Delete(fullTPATH);
- }
- }
- }
- /// <summary>
- /// 返回文件大小KB
- /// </summary>
- /// <param name="_filepath">文件相对路径</param>
- /// <returns>int</returns>
- public static int GetFileSize(string _filepath)
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- return 0;
- }
- string fullpath = GetMapPath(_filepath);
- if (File.Exists(fullpath))
- {
- FileInfo fileInfo = new FileInfo(fullpath);
- return ((int)fileInfo.Length) / 1024;
- }
- return 0;
- }
- /// <summary>
- /// 返回文件扩展名,不含“.”
- /// </summary>
- /// <param name="_filepath">文件全名称</param>
- /// <returns>string</returns>
- public static string GetFileExt(string _filepath)
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- return "";
- }
- if (_filepath.LastIndexOf(".") > 0)
- {
- return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名,不含“.”
- }
- return "";
- }
- /// <summary>
- /// 返回文件名,不含路径
- /// </summary>
- /// <param name="_filepath">文件相对路径</param>
- /// <returns>string</returns>
- public static string GetFileName(string _filepath)
- {
- return _filepath.Substring(_filepath.LastIndexOf(@"/") + 1);
- }
- /// <summary>
- /// 文件是否存在
- /// </summary>
- /// <param name="_filepath">文件相对路径</param>
- /// <returns>bool</returns>
- public static bool FileExists(string _filepath)
- {
- string fullpath = GetMapPath(_filepath);
- if (File.Exists(fullpath))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获得远程字符串
- /// </summary>
- public static string GetDomainStr(string key, string uriPath)
- {
- string result = CacheHelper.Get(key) as string;
- if (result == null)
- {
- System.Net.WebClient client = new System.Net.WebClient();
- try
- {
- client.Encoding = System.Text.Encoding.UTF8;
- result = client.DownloadString(uriPath);
- }
- catch
- {
- result = "暂时无法连接!";
- }
- CacheHelper.Insert(key, result, 60);
- }
- return result;
- }
- #endregion
- #region 读取或写入cookie
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- public static void WriteCookie(string strName, string strValue)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
- if (cookie == null)
- {
- cookie = new HttpCookie(strName);
- }
- cookie.Value = UrlEncode(strValue);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- public static void WriteCookie(string strName, string key, string strValue)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
- if (cookie == null)
- {
- cookie = new HttpCookie(strName);
- }
- cookie[key] = UrlEncode(strValue);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- public static void WriteCookie(string strName, string key, string strValue, int expires)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
- if (cookie == null)
- {
- cookie = new HttpCookie(strName);
- }
- cookie[key] = UrlEncode(strValue);
- cookie.Expires = DateTime.Now.AddMinutes(expires);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- /// <param name="strValue">过期时间(分钟)</param>
- public static void WriteCookie(string strName, string strValue, int expires)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
- if (cookie == null)
- {
- cookie = new HttpCookie(strName);
- }
- cookie.Value = UrlEncode(strValue);
- cookie.Expires = DateTime.Now.AddMinutes(expires);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 读cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <returns>cookie值</returns>
- public static string GetCookie(string strName)
- {
- if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
- return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
- return "";
- }
- /// <summary>
- /// 读cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <returns>cookie值</returns>
- public static string GetCookie(string strName, string key)
- {
- if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
- return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
- return "";
- }
- #endregion
- #region 替换指定的字符串
- /// <summary>
- /// 替换指定的字符串
- /// </summary>
- /// <param name="originalStr">原字符串</param>
- /// <param name="oldStr">旧字符串</param>
- /// <param name="newStr">新字符串</param>
- /// <returns></returns>
- public static string ReplaceStr(string originalStr, string oldStr, string newStr)
- {
- if (string.IsNullOrEmpty(oldStr))
- {
- return "";
- }
- return originalStr.Replace(oldStr, newStr);
- }
- #endregion
- #region 显示分页
- /// <summary>
- /// 返回分页页码
- /// </summary>
- /// <param name="pageSize">页面大小</param>
- /// <param name="pageIndex">当前页</param>
- /// <param name="totalCount">总记录数</param>
- /// <param name="linkUrl">链接地址,__id__代表页码</param>
- /// <param name="centSize">中间页码数量</param>
- /// <returns></returns>
- public static string OutPageList(int pageSize, int pageIndex, int totalCount, string linkUrl, int centSize)
- {
- //计算页数
- if (totalCount < 1 || pageSize < 1)
- {
- return "";
- }
- int pageCount = totalCount / pageSize;
- if (pageCount < 1)
- {
- return "";
- }
- if (totalCount % pageSize > 0)
- {
- pageCount += 1;
- }
- if (pageCount <= 1)
- {
- return "";
- }
- StringBuilder pageStr = new StringBuilder();
- string pageId = "__id__";
- string firstBtn = "<a href=\"" + ReplaceStr(linkUrl, pageId, (pageIndex - 1).ToString()) + "\">«上一页</a>";
- string lastBtn = "<a href=\"" + ReplaceStr(linkUrl, pageId, (pageIndex + 1).ToString()) + "\">下一页»</a>";
- string firstStr = "<a href=\"" + ReplaceStr(linkUrl, pageId, "1") + "\">1</a>";
- string lastStr = "<a href=\"" + ReplaceStr(linkUrl, pageId, pageCount.ToString()) + "\">" + pageCount.ToString() + "</a>";
- if (pageIndex <= 1)
- {
- firstBtn = "<span class=\"disabled\">«上一页</span>";
- }
- if (pageIndex >= pageCount)
- {
- lastBtn = "<span class=\"disabled\">下一页»</span>";
- }
- if (pageIndex == 1)
- {
- firstStr = "<span class=\"current\">1</span>";
- }
- if (pageIndex == pageCount)
- {
- lastStr = "<span class=\"current\">" + pageCount.ToString() + "</span>";
- }
- int firstNum = pageIndex - (centSize / 2); //中间开始的页码
- if (pageIndex < centSize)
- firstNum = 2;
- int lastNum = pageIndex + centSize - ((centSize / 2) + 1); //中间结束的页码
- if (lastNum >= pageCount)
- lastNum = pageCount - 1;
- pageStr.Append(firstBtn + firstStr);
- if (pageIndex >= centSize)
- {
- pageStr.Append("<span>...</span>\n");
- }
- for (int i = firstNum; i <= lastNum; i++)
- {
- if (i == pageIndex)
- {
- pageStr.Append("<span class=\"current\">" + i + "</span>");
- }
- else
- {
- pageStr.Append("<a href=\"" + ReplaceStr(linkUrl, pageId, i.ToString()) + "\">" + i + "</a>");
- }
- }
- if (pageCount - pageIndex > centSize - ((centSize / 2)))
- {
- pageStr.Append("<span>...</span>");
- }
- pageStr.Append(lastStr + lastBtn);
- return pageStr.ToString();
- }
- #endregion
- #region URL处理
- /// <summary>
- /// URL字符编码
- /// </summary>
- public static string UrlEncode(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return "";
- }
- str = str.Replace("'", "");
- return HttpContext.Current.Server.UrlEncode(str);
- }
- /// <summary>
- /// URL字符解码
- /// </summary>
- public static string UrlDecode(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return "";
- }
- return HttpContext.Current.Server.UrlDecode(str);
- }
- /// <summary>
- /// 组合URL参数
- /// </summary>
- /// <param name="_url">页面地址</param>
- /// <param name="_keys">参数名称</param>
- /// <param name="_values">参数值</param>
- /// <returns>String</returns>
- public static string CombUrlTxt(string _url, string _keys, params string[] _values)
- {
- StringBuilder urlParams = new StringBuilder();
- try
- {
- string[] keyArr = _keys.Split(new char[] { '&' });
- for (int i = 0; i < keyArr.Length; i++)
- {
- if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
- {
- _values[i] = UrlEncode(_values[i]);
- urlParams.Append(string.Format(keyArr[i], _values) + "&");
- }
- }
- if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
- urlParams.Insert(0, "?");
- }
- catch
- {
- return _url;
- }
- return _url + DelLastChar(urlParams.ToString(), "&");
- }
- #endregion
- #region URL请求数据
- /// <summary>
- /// HTTP POST方式请求数据
- /// </summary>
- /// <param name="url">URL.</param>
- /// <param name="param">POST的数据</param>
- /// <returns></returns>
- public static string HttpPost(string url, string param)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.Accept = "*/*";
- request.Timeout = 15000;
- request.AllowAutoRedirect = false;
- StreamWriter requestStream = null;
- WebResponse response = null;
- string responseStr = null;
- try
- {
- requestStream = new StreamWriter(request.GetRequestStream());
- requestStream.Write(param);
- requestStream.Close();
- response = request.GetResponse();
- if (response != null)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- responseStr = reader.ReadToEnd();
- reader.Close();
- }
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- request = null;
- requestStream = null;
- response = null;
- }
- return responseStr;
- }
- /// <summary>
- /// HTTP GET方式请求数据.
- /// </summary>
- /// <param name="url">URL.</param>
- /// <returns></returns>
- public static string HttpGet(string url)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
- request.Method = "GET";
- //request.ContentType = "application/x-www-form-urlencoded";
- request.Accept = "*/*";
- request.Timeout = 15000;
- request.AllowAutoRedirect = false;
- WebResponse response = null;
- string responseStr = null;
- try
- {
- response = request.GetResponse();
- if (response != null)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- responseStr = reader.ReadToEnd();
- reader.Close();
- }
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- request = null;
- response = null;
- }
- return responseStr;
- }
- /// <summary>
- /// 执行URL获取页面内容
- /// </summary>
- public static string UrlExecute(string urlPath)
- {
- if (string.IsNullOrEmpty(urlPath))
- {
- return "error";
- }
- StringWriter sw = new StringWriter();
- try
- {
- HttpContext.Current.Server.Execute(urlPath, sw);
- return sw.ToString();
- }
- catch (Exception)
- {
- return "error";
- }
- finally
- {
- sw.Close();
- sw.Dispose();
- }
- }
- #endregion
- }
- }
|