![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class Utils
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private static Regex RegexBr = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
public static Regex RegexFont = new Regex(@"<font color=" + "\".*?\"" + @">([\s\S]+?)</font>", Method.Utils.GetRegexCompiledOptions());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static FileVersionInfo AssemblyFileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static string TemplateCookieName = string.Format("dnttemplateid_{0}_{1}_{2}", AssemblyFileVersion.FileMajorPart, AssemblyFileVersion.FileMinorPart, AssemblyFileVersion.FileBuildPart);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 得到正则编译参数设置
/// </summary>
/// <returns></returns>
public static RegexOptions GetRegexCompiledOptions()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RegexOptions.None;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符处理#region 字符处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回字符串真实长度, 1个汉字长度为2
/// </summary>
/// <returns></returns>
public static int GetStringLength(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Encoding.Default.GetBytes(str).Length;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字符串中是否包含指定的字符
/// </summary>
/// <param name="str">被包含的字符</param>
/// <param name="stringarray">字符串</param>
/// <param name="strsplit">字符分隔符</param>
/// <returns>包含返回true,否则返回false</returns>
public static bool IsCompriseStr(string str, string stringarray, string strsplit)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (stringarray == "" || stringarray == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
str = str.ToLower();
string[] stringArray = Utils.SplitString(stringarray.ToLower(), strsplit);
for (int i = 0; i < stringArray.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string t1 = str;
//string t2 = stringArray[i];
if (str.IndexOf(stringArray[i]) > -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串在指定字符串数组中的位置
/// </summary>
/// <param name="strSearch">字符串</param>
/// <param name="stringArray">字符串数组</param>
/// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
/// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
public static int GetInArrayID(string strSearch, string[] stringArray, bool caseInsensetive)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i = 0; i < stringArray.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (caseInsensetive)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strSearch.ToLower() == stringArray[i].ToLower())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return i;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strSearch == stringArray[i])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return i;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
return -1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串在指定字符串数组中的位置
/// </summary>
/// <param name="strSearch">字符串</param>
/// <param name="stringArray">字符串数组</param>
/// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
public static int GetInArrayID(string strSearch, string[] stringArray)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetInArrayID(strSearch, stringArray, true);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串是否属于指定字符串数组中的一个元素
/// </summary>
/// <param name="strSearch">字符串</param>
/// <param name="stringArray">字符串数组</param>
/// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
/// <returns>判断结果</returns>
public static bool InArray(string strSearch, string[] stringArray, bool caseInsensetive)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetInArrayID(strSearch, stringArray, caseInsensetive) >= 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串是否属于指定字符串数组中的一个元素
/// </summary>
/// <param name="str">字符串</param>
/// <param name="stringarray">字符串数组</param>
/// <returns>判断结果</returns>
public static bool InArray(string str, string[] stringarray)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return InArray(str, stringarray, false);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串是否属于指定字符串数组中的一个元素
/// </summary>
/// <param name="str">字符串</param>
/// <param name="stringarray">内部以逗号分割单词的字符串</param>
/// <returns>判断结果</returns>
public static bool InArray(string str, string stringarray)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return InArray(str, SplitString(stringarray, ","), false);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串是否属于指定字符串数组中的一个元素
/// </summary>
/// <param name="str">字符串</param>
/// <param name="stringarray">内部以逗号分割单词的字符串</param>
/// <param name="strsplit">分割字符串</param>
/// <returns>判断结果</returns>
public static bool InArray(string str, string stringarray, string strsplit)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return InArray(str, SplitString(stringarray, strsplit), false);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断指定字符串是否属于指定字符串数组中的一个元素
/// </summary>
/// <param name="str">字符串</param>
/// <param name="stringarray">内部以逗号分割单词的字符串</param>
/// <param name="strsplit">分割字符串</param>
/// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
/// <returns>判断结果</returns>
public static bool InArray(string str, string stringarray, string strsplit, bool caseInsensetive)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return InArray(str, SplitString(stringarray, strsplit), caseInsensetive);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除字符串尾部的回车/换行/空格
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string RTrim(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i = str.Length; i >= 0; i--)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str.Remove(i, 1);
}
}
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清除给定字符串中的回车及换行符
/// </summary>
/// <param name="str">要清除的字符串</param>
/// <returns>清除后返回的字符串</returns>
public static string ClearBR(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Regex r = null;
Match m = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//r = new Regex(@"(\r\n)",RegexOptions.IgnoreCase);
for (m = RegexBr.Match(str); m.Success; m = m.NextMatch())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str.Replace(m.Groups[0].ToString(), "");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 自定义的替换字符串函数
/// </summary>
public static string ReplaceString(string SourceString, string SearchString, string ReplaceString, bool IsCaseInsensetive)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.Replace(SourceString, Regex.Escape(SearchString), ReplaceString, IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成指定数量的html空格符号
/// </summary>
public static string Spaces(int nSpaces)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nSpaces; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append(" ");
}
return sb.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理字符串
/// </summary>
public static string CleanInput(string strIn)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.Replace(strIn.Trim(), @"[^\w\.@-]", "");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 分割字符串
/// </summary>
public static string[] SplitString(string strContent, string strSplit)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strContent.IndexOf(strSplit) < 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] tmp =
{ strContent };
return tmp;
}
return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 分割字符串
/// </summary>
/// <returns></returns>
public static string[] SplitString(string strContent, string strSplit, int p_3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] result = new string[p_3];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string[] splited = SplitString(strContent, strSplit);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < p_3; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (i < splited.Length)
result[i] = splited[i];
else
result[i] = string.Empty;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return result;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 进行指定的替换(脏字过滤)
/// </summary>
public static string StrFilter(string str, string bantext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string text1 = "";
string text2 = "";
string[] textArray1 = SplitString(bantext, "\r\n");
for (int num1 = 0; num1 < textArray1.Length; num1++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
text1 = textArray1[num1].Substring(0, textArray1[num1].IndexOf("="));
text2 = textArray1[num1].Substring(textArray1[num1].IndexOf("=") + 1);
str = str.Replace(text1, text2);
}
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 过滤输入信息
/// </summary>
/// <param name="text">内容</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string FilterInputText(string text, int maxLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 为脚本替换特殊字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ReplaceStrToScript(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str.Replace("\\", "\\\\");
str = str.Replace("'", "\\'");
str = str.Replace("\"", "\\\"");
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除最后一个字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ClearLastChar(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (str == "")
return "";
else
return str.Substring(0, str.Length - 1);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将全角数字转换为数字
/// </summary>
/// <param name="SBCCase"></param>
/// <returns></returns>
public static string SBCCaseToNumberic(string SBCCase)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char[] c = SBCCase.ToCharArray();
for (int i = 0; i < c.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (b[1] == 255)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
b[0] = (byte)(b[0] + 32);
b[1] = 0;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
return new string(c);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将字符串转换为Color
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static Color ToColor(string color)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int red, green, blue = 0;
char[] rgb;
color = color.TrimStart('#');
color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
switch (color.Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 3:
rgb = color.ToCharArray();
red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
return Color.FromArgb(red, green, blue);
case 6:
rgb = color.ToCharArray();
red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
return Color.FromArgb(red, green, blue);
default:
return Color.FromName(color);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
/// </summary>
/// <param name="strNumber">要确认的字符串数组</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumericArray(string[] strNumber)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return TypeParse.IsNumericArray(strNumber);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
截取字符串函数#region 截取字符串函数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从字符串的指定位置截取指定长度的子字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">子字符串的起始位置</param>
/// <param name="length">子字符串的长度</param>
/// <returns>子字符串</returns>
public static string CutString(string str, int startIndex, int length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (startIndex >= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (length < 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = length * -1;
if (startIndex - length < 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = startIndex;
startIndex = 0;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
startIndex = startIndex - length;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (startIndex > str.Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (length < 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (length + startIndex > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = length + startIndex;
startIndex = 0;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (str.Length - startIndex < length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = str.Length - startIndex;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str.Substring(startIndex, length);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从字符串的指定位置开始截取到字符串结尾的了符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">子字符串的起始位置</param>
/// <returns>子字符串</returns>
public static string CutString(string str, int startIndex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CutString(str, startIndex, str.Length);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字符串如果操过指定长度则将超出的部分用指定字符串代替
/// </summary>
/// <param name="p_SrcString">要检查的字符串</param>
/// <param name="p_Length">指定长度</param>
/// <param name="p_TailString">用于替换的字符串</param>
/// <returns>截取后的字符串</returns>
public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetSubString(p_SrcString, 0, p_Length, p_TailString);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 取指定长度的字符串
/// </summary>
/// <param name="p_SrcString">要检查的字符串</param>
/// <param name="p_StartIndex">起始位置</param>
/// <param name="p_Length">指定长度</param>
/// <param name="p_TailString">用于替换的字符串</param>
/// <returns>截取后的字符串</returns>
public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
取指定长度的字符串#region 取指定长度的字符串
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string myResult = p_SrcString;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") ||
System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//当截取的起始位置超出字段串长度时
if (p_StartIndex >= p_SrcString.Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return p_SrcString.Substring(p_StartIndex,
((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (p_Length >= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//当字符串长度大于起始位置
if (bsSrcString.Length > p_StartIndex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int p_EndIndex = bsSrcString.Length;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//当要截取的长度在字符串的有效长度范围内
if (bsSrcString.Length > (p_StartIndex + p_Length))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p_EndIndex = p_Length + p_StartIndex;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //当不在有效范围内时,只取到字符串的结尾
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
p_Length = bsSrcString.Length - p_StartIndex;
p_TailString = "";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int nRealLength = p_Length;
int[] anResultFlag = new int[p_Length];
byte[] bsResult = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int nFlag = 0;
for (int i = p_StartIndex; i < p_EndIndex; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (bsSrcString[i] > 127)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nFlag++;
if (nFlag == 3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nFlag = 1;
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nFlag = 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
anResultFlag[i] = nFlag;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nRealLength = p_Length + 1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
bsResult = new byte[nRealLength];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myResult = Encoding.Default.GetString(bsResult);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myResult = myResult + p_TailString;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return myResult;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 截取字符串函数
/// </summary>
/// <param name="str">所要截取的字符串</param>
/// <param name="num">截取字符串的长度</param>
/// <returns></returns>
static public string GetSubString(string str, int num)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
return (str.Length > num) ? str.Substring(0, num) + "
" : str;
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
文件及文件夹方法#region 文件及文件夹方法
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 创建一个新文件夹
/// </summary>
/// <param name="P_name">指定的文件夹名称</param>
/// <returns></returns>
static public bool CreateFolder(string P_folderName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string path = HttpContext.Current.Server.MapPath(P_folderName);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(path);
return true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('已存在相同的文件夹名,请取另一个文件夹名!');</script>");
return false;
}
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 创建一个新文件夹
/// </summary>
/// <param name="P_name">指定的文件夹名称</param>
/// <returns></returns>
static public bool CreateFolder2(string P_folderName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string path = HttpContext.Current.Server.MapPath(P_folderName);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(path);
return true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除一个指定的文件夹
/// </summary>
/// <param name="P_folderName"></param>
/// <returns></returns>
static public bool DelFolder(string P_folderName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string V_path = HttpContext.Current.Server.MapPath(P_folderName);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Directory.Exists(V_path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.Delete(V_path, true);
return true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('文件夹不存在!');</script>");
return false;
}
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 显示文件夹中下的所有文件名
/// </summary>
/// <param name="P_folderName"></param>
/// <returns></returns>
static public bool AllFileShow(string P_folderName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(P_folderName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("文件夹不存在,请重新输入一个存在的文件夹名!");
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(P_folderName));
FileInfo[] file = di.GetFiles("*.*");
for (int i = 0; i < file.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write(file[i] + "<br>");
}
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 只显示以gif、jpg、bmp、png格式的文件夹下的所有此类图片
/// </summary>
/// <param name="P_folderName"></param>
/// <returns></returns>
static public bool ImgFileShow(string P_folderName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(P_folderName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("文件夹不存在,请重新输入一个存在的文件夹名!");
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(P_folderName));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] filters = new string[]
{ "*.gif", "*.jpg", "*.bmp", "*.png" };
foreach (string filter in filters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfo[] file = di.GetFiles(filter);
for (int i = 0; i < file.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write(file[i] + "<br>");
}
}
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 通过图片文件的前缀名删除图片
/// </summary>
/// <param name="P_folderName">指定的文件夹</param>
/// <param name="P_filePrefix">图片名前缀</param>
/// <returns></returns>
static public bool DelImgByPrefix(string P_folderName, string P_filePrefix)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(P_folderName)))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("文件夹不存在,请重新输入一个存在的文件夹名!");
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(P_folderName));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] filters = new string[]
{ P_filePrefix + "*.gif", P_filePrefix + "*.jpg", P_filePrefix + "*.bmp", P_filePrefix + "*.png" };
foreach (string filter in filters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfo[] file = di.GetFiles(filter);
for (int i = 0; i < file.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//HttpContext.Current.Response.Write(file[i] + "<br>");
File.Delete(HttpContext.Current.Server.MapPath(P_folderName + file[i].ToString()));
}
}
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除文件
/// </summary>
/// <param name="P_filePath">指定的文件路径</param>
/// <returns></returns>
static public bool DelFile(string P_filePath)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!File.Exists(HttpContext.Current.Server.MapPath(P_filePath)))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("文件路径不正确!");
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(HttpContext.Current.Server.MapPath(P_filePath));
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">指定的路径</param>
/// <returns>绝对路径</returns>
public static string GetMapPath(string strPath)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (HttpContext.Current != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 建立文件夹
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool CreateDir(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Utils.MakeSureDirectoryPathExists(name);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回文件是否存在
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否存在</returns>
public static bool FileExists(string filename)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return System.IO.File.Exists(filename);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 以指定的ContentType输出指定文件文件
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="filename">输出的文件名</param>
/// <param name="filetype">将文件输出时设置的ContentType</param>
public static void ResponseFile(string filepath, string filename, string filetype)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Stream iStream = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 缓冲区为10k
byte[] buffer = new Byte[10000];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 文件长度
int length;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 需要读的数据长度
long dataToRead;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 打开文件
iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 需要读的数据长度
dataToRead = iStream.Length;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HttpContext.Current.Response.ContentType = filetype;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Utils.UrlEncode(filename.Trim()).Replace("+", " "));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (dataToRead > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 检查客户端是否还处于连接状态
if (HttpContext.Current.Response.IsClientConnected)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = iStream.Read(buffer, 0, 10000);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 如果不再连接则跳出死循环
dataToRead = -1;
}
}
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("Error : " + ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (iStream != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 关闭文件
iStream.Close();
}
}
HttpContext.Current.Response.End();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断文件名是否为浏览器可以直接显示的图片文件名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否可以直接显示</returns>
public static bool IsImgFilename(string filename)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
filename = filename.Trim();
if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 备份文件
/// </summary>
/// <param name="sourceFileName">源文件名</param>
/// <param name="destFileName">目标文件名</param>
/// <param name="overwrite">当目标文件存在时是否覆盖</param>
/// <returns>操作是否成功</returns>
public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!System.IO.File.Exists(sourceFileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new FileNotFoundException(sourceFileName + "文件不存在!");
}
if (!overwrite && System.IO.File.Exists(destFileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.File.Copy(sourceFileName, destFileName, true);
return true;
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 备份文件,当目标文件存在时覆盖
/// </summary>
/// <param name="sourceFileName">源文件名</param>
/// <param name="destFileName">目标文件名</param>
/// <returns>操作是否成功</returns>
public static bool BackupFile(string sourceFileName, string destFileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return BackupFile(sourceFileName, destFileName, true);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 恢复文件
/// </summary>
/// <param name="backupFileName">备份文件名</param>
/// <param name="targetFileName">要恢复的文件名</param>
/// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param>
/// <returns>操作是否成功</returns>
public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!System.IO.File.Exists(backupFileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new FileNotFoundException(backupFileName + "文件不存在!");
}
if (backupTargetFileName != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!System.IO.File.Exists(targetFileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new FileNotFoundException(targetFileName + "文件不存在!无法备份此文件!");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.File.Copy(targetFileName, backupTargetFileName, true);
}
}
System.IO.File.Delete(targetFileName);
System.IO.File.Copy(backupFileName, targetFileName);
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool RestoreFile(string backupFileName, string targetFileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RestoreFile(backupFileName, targetFileName, null);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回URL中结尾的文件名
/// </summary>
public static string GetFilename(string url)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (url == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] strs1 = url.Split(new char[]
{ '/' });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return strs1[strs1.Length - 1].Split(new char[]
{ '?' })[0];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符加密处理#region 字符加密处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// MD5函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>MD5结果</returns>
public static string MD5(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] b = Encoding.Default.GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for (int i = 0; i < b.Length; i++)
ret += b[i].ToString("x").PadLeft(2, '0');
return ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// SHA256函数
/// </summary>
/// /// <param name="str">原始字符串</param>
/// <returns>SHA256结果</returns>
public static string SHA256(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符判断处理#region 字符判断处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object Expression)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return TypeParse.IsNumeric(Expression);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断对象是否为Double类型的数字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsDouble(object Expression)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return TypeParse.IsDouble(Expression);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断字符串是否是yy-mm-dd字符串
/// </summary>
/// <param name="str">待判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsDateString(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="strEmail">要判断的email字符串</param>
/// <returns>判断结果</returns>
public static bool IsValidEmail(string strEmail)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool IsValidDoEmail(string strEmail)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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})(\]?)$");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检测是否是正确的Url
/// </summary>
/// <param name="strUrl">要验证的Url</param>
/// <returns>判断结果</returns>
public static bool IsURL(string strUrl)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string GetEmailHostName(string strEmail)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strEmail.IndexOf("@") < 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断是否为base64字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsBase64String(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//A-Z, a-z, 0-9, +, /, =
return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeSqlString(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检测是否有危险的可能用于链接的字符串
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeUserInfoString(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证是否为正整数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsInt(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Regex.IsMatch(str, @"^[0-9]*$");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断输入是否数字
/// </summary>
/// <param name="num">要判断的字符串</param>
/// <returns></returns>
static public bool VldInt(string num)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Convert.ToInt32(num);
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch
{ return false; }
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool IsRuleTip(Hashtable NewHash, string ruletype, out string key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
key = "";
foreach (DictionaryEntry str in NewHash)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] single = SplitString(str.Value.ToString(), "\r\n");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string strs in single)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strs != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (ruletype.Trim().ToLower())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "email":
if (IsValidDoEmail(strs.ToString()) == false)
throw new Exception();
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "ip":
if (IsIPSect(strs.ToString()) == false)
throw new Exception();
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "timesect":
string[] splitetime = strs.Split('-');
if (Utils.IsTime(splitetime[1].ToString()) == false || Utils.IsTime(splitetime[0].ToString()) == false)
throw new Exception();
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
key = str.Key.ToString();
return false;
}
}
return true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
日期时间函数#region 日期时间函数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
/// </summary>
public static string[] Monthes
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return new string[]
{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准日期格式string
/// </summary>
public static string GetDate()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("yyyy-MM-dd");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回指定日期格式
/// </summary>
public static string GetDate(string datetimestr, string replacestr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (datetimestr == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return replacestr;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (datetimestr.Equals(""))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return replacestr;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return replacestr;
}
return datetimestr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准时间格式string
/// </summary>
public static string GetTime()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("HH:mm:ss");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准时间格式string
/// </summary>
public static string GetDateTime()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回相对于当前时间的相对天数
/// </summary>
public static string GetDateTime(int relativeday)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.AddDays(relativeday).ToString("yyyy-MM-dd HH:mm:ss");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准时间格式string
/// </summary>
public static string GetDateTimeF()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准时间
/// </sumary>
public static string GetStandardDateTime(string fDateTime, string formatStr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (fDateTime == "0000-0-0 0:00:00")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return fDateTime;
}
DateTime s = Convert.ToDateTime(fDateTime);
return s.ToString(formatStr);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回标准时间 yyyy-MM-dd HH:mm:ss
/// </sumary>
public static string GetStandardDateTime(string fDateTime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
/// <returns></returns>
public static bool IsTime(string timeval)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string AdDeTime(int times)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string newtime = (DateTime.Now).AddMinutes(times).ToString();
return newtime;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
SQL字符处理#region SQL字符处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 改正sql语句中的转义字符
/// </summary>
public static string mashSQL(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (str == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str2 = "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str.Replace("\'", "'");
str2 = str;
}
return str2;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 替换sql语句中的有问题符号
/// </summary>
public static string ChkSQL(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (str == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str2 = "";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str.Replace("'", "''");
str2 = str;
}
return str2;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
简繁中文函数#region 简繁中文函数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 转换为简体中文
/// </summary>
public static string ToSChinese(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 转换为繁体中文
/// </summary>
public static string ToTChinese(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符编码处理#region 字符编码处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回 HTML 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string HtmlEncode(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HttpUtility.HtmlEncode(str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回 HTML 字符串的解码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string HtmlDecode(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HttpUtility.HtmlDecode(str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回 URL 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string UrlEncode(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HttpUtility.UrlEncode(str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回 URL 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string UrlDecode(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return HttpUtility.UrlDecode(str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
UTF-8编码#region UTF-8编码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
posted @
2009-01-08 09:08
强悍的抽屉
阅读(
1122)
评论()
编辑
收藏
举报