2061 lines
72 KiB
C#
2061 lines
72 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Web;
|
||
|
||
namespace Mtxfw.Utility
|
||
{
|
||
public class Utils
|
||
{
|
||
#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 对象转换处理
|
||
/// <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>
|
||
/// 截取字符串长度,超出部分使用后缀suffix代替,比如abcdevfddd取前3位,后面使用...代替
|
||
/// </summary>
|
||
/// <param name="orginStr"></param>
|
||
/// <param name="length"></param>
|
||
/// <param name="suffix"></param>
|
||
/// <returns></returns>
|
||
public static string SubStrAddSuffix(string orginStr, int length, string suffix)
|
||
{
|
||
string ret = orginStr;
|
||
if (orginStr.Length > length)
|
||
{
|
||
ret = orginStr.Substring(0, length) + suffix;
|
||
}
|
||
return ret;
|
||
}
|
||
/// <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>
|
||
/// 检测是否符合email格式
|
||
/// </summary>
|
||
/// <param name="strEmail">要判断的email字符串</param>
|
||
/// <returns>判断结果</returns>
|
||
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})(\]?)$");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检测是否是正确的Url
|
||
/// </summary>
|
||
/// <param name="strUrl">要验证的Url</param>
|
||
/// <returns>判断结果</returns>
|
||
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\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
|
||
}
|
||
|
||
/// <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>
|
||
/// 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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将对象转换为Int类型
|
||
/// </summary>
|
||
/// <param name="o"></param>
|
||
/// <returns></returns>
|
||
public static int ObjToInt(object obj)
|
||
{
|
||
if (isNumber(obj))
|
||
{
|
||
return int.Parse(obj.ToString());
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 判断对象是否可以转成int型
|
||
/// </summary>
|
||
/// <param name="o"></param>
|
||
/// <returns></returns>
|
||
public static bool isNumber(object o)
|
||
{
|
||
int tmpInt;
|
||
if (o == null)
|
||
{
|
||
return false;
|
||
}
|
||
if (o.ToString().Trim().Length == 0)
|
||
{
|
||
return false;
|
||
}
|
||
if (!int.TryParse(o.ToString(), out tmpInt))
|
||
{
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 分割字符串
|
||
/// <summary>
|
||
/// 分割字符串
|
||
/// </summary>
|
||
public static string[] SplitString(string strContent, string strSplit)
|
||
{
|
||
if (!string.IsNullOrEmpty(strContent))
|
||
{
|
||
if (strContent.IndexOf(strSplit, StringComparison.Ordinal) < 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)
|
||
{
|
||
if (str.Length < 1)
|
||
{
|
||
return "";
|
||
}
|
||
return str.Substring(0, str.LastIndexOf(","));
|
||
}
|
||
#endregion
|
||
|
||
#region 删除开始的指定字符后的字符
|
||
/// <summary>
|
||
/// 删除开始和结尾的指定字符后的字符
|
||
/// </summary>
|
||
public static string DelStartChar(string str, char strchar)
|
||
{
|
||
if (string.IsNullOrEmpty(str))
|
||
return "";
|
||
if (str.LastIndexOf(strchar) >= 0 && str.IndexOf(strchar) == 0)
|
||
{
|
||
return str.Trim(strchar);
|
||
}
|
||
str = DelLastChar(str, strchar.ToString());
|
||
return str;
|
||
}
|
||
#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>
|
||
/// 删除开始和结尾的指定字符后的字符
|
||
/// </summary>
|
||
public static string DelStartLastChar(string str, char strchar)
|
||
{
|
||
if (string.IsNullOrEmpty(str))
|
||
return "";
|
||
if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1 && str.IndexOf(strchar) == 0)
|
||
{
|
||
return str.Trim(strchar);
|
||
}
|
||
str = DelLastChar(str, strchar.ToString());
|
||
return str;
|
||
}
|
||
|
||
///<summary>
|
||
/// 截前后字符(串)
|
||
///</summary>
|
||
///<param name="val">原字符串</param>
|
||
///<param name="str">要截掉的字符串</param>
|
||
///<param name="all">是否贪婪</param>
|
||
///<returns></returns>
|
||
public static string DelStartLastString(string val, string str, bool all = false)
|
||
{
|
||
return Regex.Replace(val, @"(^(" + str + ")" + (all ? "*" : "") + "|(" + str + ")" + (all ? "*" : "") + "$)", "");
|
||
}
|
||
#endregion
|
||
|
||
#region 添加开始和结尾的指定字符后的字符
|
||
/// <summary>
|
||
/// 添加开始和结尾的指定字符后的字符
|
||
/// </summary>
|
||
/// <param name="str">字符串</param>
|
||
/// <param name="strchar">分隔符</param>
|
||
/// <returns></returns>
|
||
public static string AddStartLastChar(string str, string strchar)
|
||
{
|
||
if (string.IsNullOrEmpty(str))
|
||
return "";
|
||
if (str.IndexOf(strchar) != 0) str = strchar + str;
|
||
if (str.LastIndexOf(strchar) != str.Length - 1) str = str + 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(bool Sleep = false)
|
||
{
|
||
#region
|
||
if (Sleep) System.Threading.Thread.Sleep(3);
|
||
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("yyMMddHHmmssfff");//yyyyMMddHHmmssms
|
||
return num + Number(2, true).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 GetObjectContent(object obj)
|
||
{
|
||
string result = String.Empty;
|
||
if (obj != null)
|
||
{
|
||
string str = Utils.ObjectToStr(obj);
|
||
if (!string.IsNullOrEmpty(str))
|
||
{
|
||
result = str;
|
||
}
|
||
else
|
||
{
|
||
result = "<p style='color:#8080809c;font-style: italic;'><b>暂无</b></p>";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
result = "<p style='color:#8080809c;font-style: italic;'><b>暂无</b></p>";
|
||
}
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
#region 截取字符长度
|
||
/// <summary>
|
||
/// 截取字符长度
|
||
/// </summary>
|
||
/// <param name="inputString">字符</param>
|
||
/// <param name="len">长度</param>
|
||
/// <returns></returns>
|
||
public static string CutString(string inputString, int len, string str = "…")
|
||
{
|
||
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 = Encoding.Default.GetBytes(inputString);
|
||
if (mybyte.Length > len) tempString += str;
|
||
return tempString;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 裁减字符串 - 优化版 liwh - 20160523
|
||
/// </summary>
|
||
/// <param name="originalText">被裁减字符串</param>
|
||
/// <param name="bytesAfterCut">需保留的字节数</param>
|
||
/// <param name="tail">截取后的尾巴 如:。。。</param>
|
||
/// <returns></returns>
|
||
public static string GetOptimizedText(string originalText, int bytesAfterCut, string tail = "")
|
||
{
|
||
string optimizedText = originalText;
|
||
byte[] val = Encoding.Default.GetBytes(originalText);
|
||
if (val.Length > bytesAfterCut)
|
||
{
|
||
int left = bytesAfterCut / 2;
|
||
int right = bytesAfterCut;
|
||
left = left > originalText.Length ? originalText.Length : left;
|
||
right = right > originalText.Length ? originalText.Length : right;
|
||
while (left < right - 1)
|
||
{
|
||
int mid = (left + right) / 2;
|
||
if (Encoding.Default.GetBytes(originalText.Substring(0, mid)).Length >
|
||
bytesAfterCut)
|
||
{
|
||
right = mid;
|
||
}
|
||
else
|
||
{
|
||
left = mid;
|
||
}
|
||
}
|
||
byte[] rightVal = Encoding.Default.GetBytes(originalText.Substring(0, right));
|
||
if (rightVal.Length == bytesAfterCut)
|
||
{
|
||
optimizedText = originalText.Substring(0, right) + tail;
|
||
}
|
||
else
|
||
{
|
||
optimizedText = originalText.Substring(0, left) + tail;
|
||
}
|
||
}
|
||
return optimizedText;
|
||
}
|
||
#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.Replace(" ", "");
|
||
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(">", ">");
|
||
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 filepath)
|
||
{
|
||
return GetMapPath(filepath);
|
||
}
|
||
#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 读取或写入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] ?? 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("<span>共" + totalCount + "记录</span>");
|
||
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 显示ajax记录分页
|
||
/// <summary>
|
||
/// 返回ajax分页页码
|
||
/// </summary>
|
||
/// <param name="pageSize">页面大小</param>
|
||
/// <param name="pageIndex">当前页</param>
|
||
/// <param name="totalCount">总记录数</param>
|
||
/// <param name="pageCount">返回分页数</param>
|
||
/// <returns></returns>
|
||
public static string OutPageList(int pageSize, int pageIndex, int totalCount, out int pageCount, int centSize = 8)
|
||
{
|
||
//计算页数
|
||
pageCount = totalCount / pageSize;
|
||
if (totalCount < 1 || pageSize < 1)
|
||
{
|
||
return "";
|
||
}
|
||
if (pageCount < 1)
|
||
{
|
||
return "";
|
||
}
|
||
if (totalCount % pageSize > 0)
|
||
{
|
||
pageCount += 1;
|
||
}
|
||
if (pageCount <= 1)
|
||
{
|
||
return "";
|
||
}
|
||
StringBuilder pageStr = new StringBuilder();
|
||
string firstBtn = "<a data-page='" + (pageIndex - 1).ToString() + "'><<上一页</a>";
|
||
string lastBtn = "<a data-page='" + (pageIndex + 1).ToString() + "'>下一页>></a>";
|
||
string firstStr = "<a data-page='1'>1</a>";
|
||
string lastStr = "<a data-page='" + 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>";
|
||
}
|
||
pageStr.Append("<div>");
|
||
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("<span>共" + totalCount + "记录</span>");
|
||
pageStr.Append(firstBtn + firstStr);
|
||
if (pageIndex >= centSize)
|
||
{
|
||
pageStr.Append("<span>...</span>");
|
||
}
|
||
for (int i = firstNum; i <= lastNum; i++)
|
||
{
|
||
if (i == pageIndex)
|
||
{
|
||
pageStr.Append("<span class='current'>" + i + "</span>");
|
||
}
|
||
else
|
||
{
|
||
pageStr.Append("<a data-page='" + i.ToString() + "'>" + i + "</a>");
|
||
}
|
||
}
|
||
if (pageCount - pageIndex > centSize - ((centSize / 2)))
|
||
{
|
||
pageStr.Append("<span>...</span>");
|
||
}
|
||
pageStr.Append(lastStr + lastBtn);
|
||
pageStr.Append("</div>");
|
||
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
|
||
|
||
#region 操作权限菜单
|
||
/// <summary>
|
||
/// 获取操作权限
|
||
/// </summary>
|
||
/// <returns>Dictionary</returns>
|
||
public static Dictionary<string, string> ActionType()
|
||
{
|
||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
dic.Add("Show", "显示");
|
||
dic.Add("View", "查看");
|
||
dic.Add("Add", "添加");
|
||
dic.Add("Edit", "修改");
|
||
dic.Add("Delete", "删除");
|
||
dic.Add("Audit", "审核");
|
||
dic.Add("Reply", "回复");
|
||
dic.Add("Confirm", "确认");
|
||
dic.Add("Cancel", "取消");
|
||
dic.Add("Invalid", "作废");
|
||
dic.Add("Build", "生成");
|
||
dic.Add("Instal", "安装");
|
||
dic.Add("Unload", "卸载");
|
||
dic.Add("Back", "备份");
|
||
dic.Add("Restore", "还原");
|
||
dic.Add("Replace", "替换");
|
||
return dic;
|
||
}
|
||
#endregion
|
||
|
||
#region 替换URL
|
||
/// <summary>
|
||
/// 替换扩展名
|
||
/// </summary>
|
||
public static string GetUrlExtension(string urlPage, string staticExtension)
|
||
{
|
||
int indexNum = urlPage.LastIndexOf('.');
|
||
if (indexNum > 0)
|
||
{
|
||
return urlPage.Replace(urlPage.Substring(indexNum), "." + staticExtension);
|
||
}
|
||
return urlPage;
|
||
}
|
||
/// <summary>
|
||
/// 替换扩展名,如没有扩展名替换默认首页
|
||
/// </summary>
|
||
public static string GetUrlExtension(string urlPage, string staticExtension, bool defaultVal)
|
||
{
|
||
int indexNum = urlPage.LastIndexOf('.');
|
||
if (indexNum > 0)
|
||
{
|
||
return urlPage.Replace(urlPage.Substring(indexNum), "." + staticExtension);
|
||
}
|
||
if (defaultVal)
|
||
{
|
||
if (urlPage.EndsWith("/"))
|
||
{
|
||
return urlPage + "index." + staticExtension;
|
||
}
|
||
else
|
||
{
|
||
return urlPage + "/index." + staticExtension;
|
||
}
|
||
}
|
||
return urlPage;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取配置文件Key对应Value值
|
||
/// <summary>
|
||
/// 获取配置文件Key对应Value值
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public static string GetConfigValue(string key)
|
||
{
|
||
return ConfigurationManager.AppSettings[key].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 计算两个日期之间相差的天数
|
||
/// <summary>
|
||
/// 计算两个日期之间相差的天数
|
||
/// </summary>
|
||
/// <param name="dateStart">开始时间</param>
|
||
/// <param name="dateEnd">结束时间</param>
|
||
/// <returns></returns>
|
||
public static int DateDiff(DateTime dateStart, DateTime dateEnd)
|
||
{
|
||
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
|
||
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
|
||
TimeSpan sp = end.Subtract(start);
|
||
return sp.Days;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算两个日期之间相差的秒数
|
||
/// </summary>
|
||
/// <param name="dateStart">开始时间</param>
|
||
/// <param name="dateEnd">结束时间</param>
|
||
/// <returns></returns>
|
||
public static int DateDiffToSeconds(DateTime dateStart, DateTime dateEnd)
|
||
{
|
||
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
|
||
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
|
||
TimeSpan sp = end.Subtract(start);
|
||
return sp.Seconds;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获得两个日期的间隔
|
||
/// </summary>
|
||
/// <param name="dateTime1">日期一。</param>
|
||
/// <param name="dateTime2">日期二。</param>
|
||
/// <returns>日期间隔TimeSpan。</returns>
|
||
public static TimeSpan DateDiff2(DateTime dateTime1, DateTime dateTime2)
|
||
{
|
||
TimeSpan ts1 = new TimeSpan(dateTime1.Ticks);
|
||
TimeSpan ts2 = new TimeSpan(dateTime2.Ticks);
|
||
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
||
return ts;
|
||
}
|
||
#endregion
|
||
|
||
#region 将传入的字符串中间部分字符替换成特殊字符
|
||
/// <summary>
|
||
/// 将传入的字符串中间部分字符替换成特殊字符
|
||
/// </summary>
|
||
/// <param name="value">需要替换的字符串</param>
|
||
/// <param name="startLen">前保留长度</param>
|
||
/// <param name="endLen">尾保留长度</param>
|
||
/// <param name="replaceChar">特殊字符</param>
|
||
/// <returns>被特殊字符替换的字符串</returns>
|
||
public static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
|
||
{
|
||
try
|
||
{
|
||
int lenth = value.Length - startLen - endLen;
|
||
string replaceStr = value.Substring(startLen, lenth);
|
||
string specialStr = string.Empty;
|
||
for (int i = 0; i < replaceStr.Length; i++)
|
||
{
|
||
specialStr += specialChar;
|
||
}
|
||
value = value.Replace(replaceStr, specialStr);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return value;
|
||
}
|
||
return value;
|
||
}
|
||
#endregion
|
||
|
||
#region 在指定字符串中间插入换行符
|
||
public static string BreakLongString(string SubjectString, int lineLength, string str = "\n")
|
||
{
|
||
StringBuilder sb = new StringBuilder(); int sumCount = 0;
|
||
ArrayList indexList = buildInsertIndexList(SubjectString, lineLength);
|
||
byte[] bytes = Encoding.Default.GetBytes(SubjectString); int strCount = Encoding.Default.GetByteCount(SubjectString);
|
||
for (int i = 0; i < indexList.Count; i++)
|
||
{
|
||
string s = indexList[i].ToString();
|
||
if (!string.IsNullOrEmpty(s))
|
||
{
|
||
int index = Utils.StrToInt(Utils.SplitString(s, ",")[0], 0);
|
||
int count = Utils.StrToInt(Utils.SplitString(s, ",")[1], 0);
|
||
string cutString = Encoding.Default.GetString(bytes, index, count);
|
||
sumCount += Encoding.Default.GetByteCount(cutString);
|
||
sb.Append(cutString);
|
||
if (i != indexList.Count - 1) sb.Append(str);
|
||
}
|
||
}
|
||
|
||
if (strCount > sumCount)
|
||
{
|
||
string cutString = Encoding.Default.GetString(bytes, sumCount, strCount - sumCount);
|
||
sb.Append(str + cutString);
|
||
}
|
||
return DelLastChar(sb.ToString(), str);
|
||
}
|
||
|
||
private static ArrayList buildInsertIndexList(string str, int maxLen)
|
||
{
|
||
int nowLen = 0; int index = 0; int count = 0;
|
||
ArrayList list = new ArrayList();
|
||
byte[] s = new ASCIIEncoding().GetBytes(str);
|
||
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
if ((int)s[i] == 63)
|
||
nowLen += 2;
|
||
else
|
||
nowLen += 1;
|
||
|
||
if (nowLen >= maxLen)
|
||
{
|
||
index += count; count = nowLen; nowLen = 0;
|
||
list.Add(index + "," + count);
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取字符串长度
|
||
/// <summary>
|
||
/// 获取字符串长度
|
||
/// </summary>
|
||
/// <param name="str">字符串</param>
|
||
/// <returns></returns>
|
||
public static int GetStringLength(string str)
|
||
{
|
||
if (str.Length == 0)
|
||
return 0;
|
||
|
||
int tempLen = 0;
|
||
byte[] s = new ASCIIEncoding().GetBytes(str);
|
||
for (int i = 0; i < s.Length; i++)
|
||
{
|
||
if ((int)s[i] == 63)
|
||
tempLen += 2;
|
||
else
|
||
tempLen += 1;
|
||
}
|
||
return tempLen;
|
||
}
|
||
#endregion
|
||
|
||
#region 移除数据表中的列
|
||
/// <summary>
|
||
/// 移除数据表中的列
|
||
/// </summary>
|
||
/// <param name="dt"></param>
|
||
/// <param name="removeColumn"></param>
|
||
/// <returns></returns>
|
||
public static DataTable RemoveColumns(DataTable dt, string[] removeColumn)
|
||
{
|
||
foreach (string column in removeColumn)
|
||
{
|
||
if (dt.Columns.Contains(column))
|
||
{
|
||
dt.Columns.Remove(column);
|
||
}
|
||
}
|
||
return dt;
|
||
}
|
||
#endregion
|
||
|
||
#region 根据datatable获得列名
|
||
/// <summary>
|
||
/// 根据datatable获得列名
|
||
/// </summary>
|
||
/// <param name="dt">表对象</param>
|
||
/// <returns>返回结果的数据列数组</returns>
|
||
public static string[] GetColumnsByDataTable(DataTable dt)
|
||
{
|
||
string[] strColumns = null;
|
||
if (dt.Columns.Count > 0)
|
||
{
|
||
int columnNum = 0;
|
||
columnNum = dt.Columns.Count;
|
||
strColumns = new string[columnNum];
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
strColumns[i] = dt.Columns[i].ColumnName;
|
||
}
|
||
}
|
||
return strColumns;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取Img的路径
|
||
/// <summary>
|
||
/// 获取Img的路径
|
||
/// </summary>
|
||
/// <param name="htmlText">Html字符串文本</param>
|
||
/// <returns>以数组形式返回图片路径</returns>
|
||
public static string[] GetHtmlImageUrlList(string htmlText)
|
||
{
|
||
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
|
||
//新建一个matches的MatchCollection对象 保存 匹配对象个数(img标签)
|
||
MatchCollection matches = regImg.Matches(htmlText);
|
||
int i = 0;
|
||
string[] sUrlList = new string[matches.Count];
|
||
//遍历所有的img标签对象
|
||
foreach (Match match in matches)
|
||
{
|
||
//获取所有Img的路径src,并保存到数组中
|
||
sUrlList[i++] = match.Groups["imgUrl"].Value;
|
||
}
|
||
return sUrlList;
|
||
}
|
||
#endregion
|
||
|
||
#region 替换编辑器中图片路径=======================
|
||
/// <summary>
|
||
/// 替换编辑器中图片路径
|
||
/// </summary>
|
||
/// <param name="content">内容</param>
|
||
/// <param name="url">图片网址</param>
|
||
/// <returns></returns>
|
||
public static string ReplaceImage(string content, string url = "")
|
||
{
|
||
if (string.IsNullOrEmpty(content))
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
|
||
MatchCollection m = reg.Matches(content);
|
||
foreach (Match math in m)
|
||
{
|
||
string imgUri = math.Groups[1].Value;
|
||
if (!(imgUri.StartsWith("http") || imgUri.StartsWith("https")))
|
||
{
|
||
string newImgPath = url + imgUri;
|
||
if (newImgPath != string.Empty)
|
||
{
|
||
content = content.Replace(imgUri, newImgPath);
|
||
}
|
||
}
|
||
}
|
||
return content;
|
||
}
|
||
#endregion
|
||
|
||
#region 根据文件后缀获取文件类型
|
||
/// <summary>
|
||
/// 根据文件后缀获取文件类型
|
||
/// </summary>
|
||
/// <param name="suffix"></param>
|
||
/// <returns></returns>
|
||
public static string GetFileType(string suffix)
|
||
{
|
||
string img_suffix = "," + ConfigurationManager.AppSettings["img_suffix"].ToString() + ",";
|
||
string video_suffix = "," + ConfigurationManager.AppSettings["video_suffix"].ToString() + ",";
|
||
string file_suffix = "," + ConfigurationManager.AppSettings["file_suffix"].ToString() + ",";
|
||
if (img_suffix.ToUpper().Contains("," + suffix.ToUpper() + ","))
|
||
{
|
||
return "img";
|
||
}
|
||
else if (video_suffix.ToUpper().Contains("," + suffix.ToUpper() + ","))
|
||
{
|
||
return "video";
|
||
}
|
||
else if (file_suffix.ToUpper().Contains("," + suffix.ToUpper() + ","))
|
||
{
|
||
return "file";
|
||
}
|
||
return "other";
|
||
}
|
||
#endregion
|
||
|
||
#region 判断文件是否为图片
|
||
/// <summary>
|
||
/// 判断文件是否为图片
|
||
/// </summary>
|
||
/// <param name="path">文件的完整路径</param>
|
||
/// <returns>返回结果</returns>
|
||
public Boolean IsImage(string path)
|
||
{
|
||
try
|
||
{
|
||
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
|
||
return true;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 根据文件头判断上传的文件类型
|
||
/// <summary>
|
||
/// 根据文件头判断上传的文件类型
|
||
/// </summary>
|
||
/// <param name="filePath">filePath是文件的完整路径 </param>
|
||
/// <returns>返回true或false</returns>
|
||
public static bool IsPicture(string filePath)
|
||
{
|
||
try
|
||
{
|
||
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||
BinaryReader reader = new BinaryReader(fs);
|
||
var buffer = reader.ReadByte();
|
||
var fileClass = buffer.ToString();
|
||
buffer = reader.ReadByte();
|
||
fileClass += buffer.ToString();
|
||
reader.Close();
|
||
fs.Close();
|
||
//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
|
||
if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677")
|
||
{
|
||
try
|
||
{
|
||
System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
|
||
return true;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region string数组转int数组
|
||
/// <summary>
|
||
/// string数组转int数组
|
||
/// </summary>
|
||
/// <param name="strArray">string数组</param>
|
||
/// <returns></returns>
|
||
public static int[] StringToIntArray(string strArray)
|
||
{
|
||
if (!string.IsNullOrEmpty(strArray))
|
||
{
|
||
try
|
||
{
|
||
var array = SplitString(DelStartLastChar(strArray, ','), ",");
|
||
return Array.ConvertAll<string, int>(array, delegate (string s) { return int.Parse(s); });
|
||
}
|
||
catch
|
||
{
|
||
return new int[0] { };
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return new int[0] { };
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 时间戳
|
||
/// <summary>
|
||
/// 获取时间戳
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string GetTimeStamp()
|
||
{
|
||
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||
return Convert.ToInt64(ts.TotalSeconds).ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// DateTime时间格式转换为Unix时间戳格式
|
||
/// </summary>
|
||
/// <param name="time"> DateTime时间格式</param>
|
||
/// <returns>Unix时间戳格式</returns>
|
||
public static int ConvertDateTimeInt(DateTime time)
|
||
{
|
||
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
||
return (int)(time - startTime).TotalSeconds;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 生成指定位数的随机数
|
||
/// <summary>
|
||
/// 该方法用于生成指定位数的随机数
|
||
/// </summary>
|
||
/// <param name="VcodeNum">参数是随机数的位数</param>
|
||
/// <returns>返回一个随机数字符串</returns>
|
||
public static string RandNum(int VcodeNum)
|
||
{
|
||
string Vchar = "0,1,2,3,4,5,6,7,8,9";
|
||
string[] VcArray = Vchar.Split(',');//拆分成数组
|
||
string VNum = "";
|
||
int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数
|
||
|
||
Random rand = new Random();
|
||
//采用一个简单的算法以保证生成随机数的不同
|
||
for (int i = 0; i < VcodeNum; i++)
|
||
{
|
||
if (temp != -1)
|
||
{
|
||
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
|
||
}
|
||
|
||
int t = rand.Next(VcArray.Length - 1);
|
||
if (temp != -1 && temp == t)
|
||
{
|
||
return RandNum(VcodeNum);
|
||
|
||
}
|
||
temp = t;
|
||
VNum += VcArray[t];
|
||
}
|
||
return VNum;
|
||
}
|
||
#endregion
|
||
|
||
#region 检测是否是http和https
|
||
/// <summary>
|
||
/// 检测是否是http和https
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <returns></returns>
|
||
public static bool ContainsHttpOrHttps(string url)
|
||
{
|
||
Regex reg = new Regex(@"http|https");
|
||
Match m = reg.Match(url);
|
||
return m.Success;
|
||
}
|
||
#endregion
|
||
|
||
#region 随机生成手机号
|
||
/// <summary>
|
||
/// 随机生成手机号
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string RandomMobile()
|
||
{
|
||
string[] telStarts = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153,180,181,182,183,185,186,176,187,188,189,177,178".Split(',');
|
||
|
||
Random ran = new Random();
|
||
int n = ran.Next(10, 1000);
|
||
int index = ran.Next(0, telStarts.Length - 1);
|
||
string first = telStarts[index];
|
||
string second = (ran.Next(100, 888) + 10000).ToString().Substring(1);
|
||
string thrid = (ran.Next(1, 9100) + 10000).ToString().Substring(1);
|
||
return first + second + thrid;
|
||
}
|
||
#endregion
|
||
|
||
#region 邮箱号中间一串改成*号
|
||
public static string ReplaceEmail(string email)
|
||
{
|
||
string s = Regex.Replace(email, @"(?<=\w{2}).*?(?=\w@)", m =>
|
||
{
|
||
return string.Join(string.Empty, Enumerable.Repeat('*', m.Value.Length));
|
||
});
|
||
return s;
|
||
}
|
||
#endregion
|
||
|
||
#region 替换手机号码中间数字为*号
|
||
public static string ReplaceMobile(string mobile)
|
||
{
|
||
return Regex.Replace(mobile, @"(?im)(\d{3})(\d{4})(\d{4})", "$1****$3");//134****0555
|
||
}
|
||
#endregion
|
||
|
||
#region 返回时间戳,毫秒
|
||
/// <summary>
|
||
/// 返回时间戳,毫秒
|
||
/// </summary>
|
||
/// <param name="time"></param>
|
||
/// <returns></returns>
|
||
public static long UtcTrick(DateTime time)
|
||
{
|
||
DateTimeOffset fs = new DateTimeOffset(time);
|
||
long trick = (fs.UtcTicks - 621355968000000000) / 10000;//秒值
|
||
return trick;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取流文件扩展名
|
||
/// <summary>
|
||
/// 获取图片流文件扩展名
|
||
/// </summary>
|
||
/// <param name="image"></param>
|
||
/// <returns></returns>
|
||
public static string GetExtension(Image image)
|
||
{
|
||
if (image != null)
|
||
{
|
||
string fileExtension = image.RawFormat.ToString().Substring(14),
|
||
jpgExtension = System.Drawing.Imaging.ImageFormat.Jpeg.Guid.ToString(),
|
||
gifExtension = System.Drawing.Imaging.ImageFormat.Gif.Guid.ToString(),
|
||
pngExtension = System.Drawing.Imaging.ImageFormat.Png.Guid.ToString(),
|
||
iconExtension = System.Drawing.Imaging.ImageFormat.Icon.Guid.ToString(),
|
||
bmpExtension = System.Drawing.Imaging.ImageFormat.Bmp.Guid.ToString(),
|
||
tiffExtension = System.Drawing.Imaging.ImageFormat.Tiff.Guid.ToString(),
|
||
wmfExtension = System.Drawing.Imaging.ImageFormat.Wmf.Guid.ToString(),
|
||
emfExtension = System.Drawing.Imaging.ImageFormat.Emf.Guid.ToString(),
|
||
exifExtension = System.Drawing.Imaging.ImageFormat.Exif.Guid.ToString();
|
||
fileExtension = fileExtension.Substring(0, fileExtension.Length - 1);
|
||
if (fileExtension == jpgExtension)
|
||
{
|
||
return ".jpg";
|
||
}
|
||
else if (fileExtension == gifExtension)
|
||
{
|
||
return ".gif";
|
||
}
|
||
else if (fileExtension == pngExtension)
|
||
{
|
||
return ".png";
|
||
}
|
||
else if (fileExtension == iconExtension)
|
||
{
|
||
return ".icon";
|
||
}
|
||
else if (fileExtension == bmpExtension)
|
||
{
|
||
return ".bmp";
|
||
}
|
||
else if (fileExtension == tiffExtension)
|
||
{
|
||
return ".tiff";
|
||
}
|
||
else if (fileExtension == wmfExtension)
|
||
{
|
||
return ".wmf";
|
||
}
|
||
else if (fileExtension == emfExtension)
|
||
{
|
||
return ".emf";
|
||
}
|
||
else if (fileExtension == exifExtension)
|
||
{
|
||
return ".exif";
|
||
}
|
||
}
|
||
return "";
|
||
}
|
||
#endregion
|
||
|
||
#region 获取文件的路劲+http
|
||
public static string GetIsFileUrl(object _fileUrl)
|
||
{
|
||
string rt = String.Empty;
|
||
string imgurl = Utils.ObjectToStr(_fileUrl);
|
||
if (!string.IsNullOrEmpty(imgurl))
|
||
{
|
||
if (imgurl.Contains("http"))
|
||
{
|
||
rt = imgurl;
|
||
}
|
||
else
|
||
{
|
||
rt = System.Configuration.ConfigurationManager.AppSettings["Web"].ToString() + imgurl;
|
||
}
|
||
}
|
||
return rt;
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
}
|
||
}
|