上传文件

来源:互联网 发布:网络用语2015最新 编辑:程序博客网 时间:2024/06/06 01:13
 

 public static string UploadFile(System.Web.UI.WebControls.FileUpload postedFile, string path, int size, string filetype)
    {
        try
        {
            string strAllPath = postedFile.PostedFile.FileName;
            //上傳文件的完整原始物理路徑               
            string strAllName = System.IO.Path.GetFileName(postedFile.PostedFile.FileName);
            //原文件名(包括格式)
            string strtype = System.IO.Path.GetExtension(postedFile.PostedFile.FileName);
            //擴展名
            string strName = System.IO.Path.GetFileNameWithoutExtension(postedFile.PostedFile.FileName);
            //文件名(不包含格式)

            string strSaveName = Guid.NewGuid() + strtype;

            if (string.IsNullOrEmpty(strName))
            {
                return "";
            }

            //上傳後的文件名稱
            if (path == "")
            {
                path = HttpContext.Current.Request.PhysicalApplicationPath + "\\ProductImages\\" + DateTime.Now.Date.ToString("yyyy/MM/dd") + "\\";
                if (System.IO.Directory.Exists(path) == false)
                {
                    System.IO.Directory.CreateDirectory(path);
                    //如果沒就創建一個目錄;
                }
            }
            string strSavePath = path + strSaveName;
            //爲文件獲取保存路徑
            int fileSize = postedFile.PostedFile.ContentLength / 1024;


            //是否限制文件類型
            if (filetype.Length > 0)
            {
                if (filetype.ToLower().Equals("default"))
                {
                    if (strtype.ToLower() != ".jpg" && strtype.ToLower() != ".png" && strtype.ToLower() != ".gif")
                    {
                        return "1";
                    }
                }
                else
                {
                    string[] uploadType = filetype.Split('|');
                    foreach (string type in uploadType)
                    {
                        if (strtype.ToLower() != type)
                        {
                            return "1";
                        }
                    }
                }
            }
            //是否限制大小
            if (size > 0)
            {
                if (fileSize <= size)
                {
                    postedFile.PostedFile.SaveAs(strSavePath);
                    //保存上傳的文件到指定的路徑
                    //Page.RegisterClientScriptBlock("", "<script>alert('上傳成功!');</script>");
                    return strSaveName;
                }
                else
                {
                    //上傳的文件大小超出
                    return "2";
                }
            }
            else
            {
                postedFile.PostedFile.SaveAs(strSavePath);
                //保存上傳的文件到指定的路徑
                return DateTime.Now.Date.ToString("yyyy/MM/dd") + "\\" + strSaveName;
            }
        }
        catch (Exception ex)
        {
            throw;
        }

 

 

 

 

 

 

 

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using Microsoft.VisualBasic;

using System.Collections.Generic;

/// <summary>
/// 工具类
/// </summary>
public class Utils
{


    /// <summary>
    /// 当前页面Meta字段内容
    /// </summary>
    protected internal string meta = "";
    /// <summary>
    /// 当前页面Link字段内容
    /// </summary>
    protected internal string link;
    /// <summary>
    /// 当前页面中增加script
    /// </summary>
    protected internal string script;
    /// <summary>
    /// 当前页面检查到的错误数
    /// </summary>
    protected internal int page_err = 0;
    /// <summary>
    /// 提示文字
    /// </summary>
    protected internal string msgbox_text = "";
    /// <summary>
    /// 是否显示回退的链接
    /// </summary>
    protected internal string msgbox_showbacklink = "true";
    /// <summary>
    /// 回退链接的内容
    /// </summary>
    protected internal string msgbox_backlink = "javascript:history.back();";
    /// <summary>
    /// 返回到的页面url地址
    /// </summary>
    protected internal string msgbox_url = "";
    /// <summary>
    ///
    /// <summary>
    /// 返回字符串真实长度, 1个汉字长度为2
    /// </summary>
    /// <returns></returns>
    public static int GetStringLength(string str)
    {
        return Encoding.Default.GetBytes(str).Length;
    }

 

    /// <summary>
    /// 得到正则编译参数设置
    /// </summary>
    /// <returns></returns>
    public static RegexOptions GetRegexCompiledOptions()
    {
        return RegexOptions.None;
    }


    /// <summary>
    /// 判断对象是否为Int32类型的数字
    /// </summary>
    /// <param name="Expression"></param>
    /// <returns></returns>
    public static bool IsNumeric(object Expression)
    {
        if (Expression != null)
        {
            string str = Expression.ToString();
            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;

    }

 

    public static string Referrer
    {
        get
        {
            Uri urlReferrer = HttpContext.Current.Request.UrlReferrer;
            if (urlReferrer == null)
            {
                return string.Empty;
            }
            return Convert.ToString(urlReferrer);
        }
    }

    public static void UserAlert(string mAlertText)
    {
        if ((HttpContext.Current.Request.Headers["dv_ajax"] != null) && ("1" == HttpContext.Current.Request.Headers["dv_ajax"].ToString()))
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.ContentEncoding = HttpContext.Current.Request.ContentEncoding;
            HttpContext.Current.Response.AppendHeader("dv_ajax", "1");
            string s = "{message:\"" + mAlertText + "\"};";
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }
        HttpContext.Current.Response.Write("<script>try{parent.document.Dvform.Submit1.disabled=false;\tparent.document.Dvform.Submit2.disabled=false;}catch(e){} alert('" + mAlertText + "');\t</script>");
        HttpContext.Current.Response.End();
    }

    public static void UserAlert(string mAlertText, string goUrl)
    {
        if ((HttpContext.Current.Request.Headers["dv_ajax"] != null) && ("1" == HttpContext.Current.Request.Headers["dv_ajax"].ToString()))
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.ContentEncoding = HttpContext.Current.Request.ContentEncoding;
            HttpContext.Current.Response.AppendHeader("dv_ajax", "1");
            string s = "{message:\"" + mAlertText + "\", goUrl:\"" + goUrl + "\"};";
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }
        HttpContext.Current.Response.Write("<script>try{parent.document.Dvform.Submit1.disabled=false;\tparent.document.Dvform.Submit2.disabled=false;}catch(e){} alert('" + mAlertText + "');if('" + goUrl + "'!='){location.href='" + goUrl + "'}else{history.go(-1);}</script>");
        HttpContext.Current.Response.End();
    }

    public static void WindowLocation(string mUrl)
    {
        if ((HttpContext.Current.Request.Headers["dv_ajax"] != null) && ("1" == HttpContext.Current.Request.Headers["dv_ajax"].ToString()))
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.ContentEncoding = HttpContext.Current.Request.ContentEncoding;
            HttpContext.Current.Response.AppendHeader("dv_ajax", "1");
            string s = "{message:\"\", goUrl:\"" + mUrl + "\"};";
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }
        HttpContext.Current.Response.Write("<script>parent.window.location.href='" + mUrl + "';</script>");
        HttpContext.Current.Response.End();
    }

    public static void WindowLocation(string mUrl, string mAlertText)
    {
        if ((HttpContext.Current.Request.Headers["dv_ajax"] != null) && ("1" == HttpContext.Current.Request.Headers["dv_ajax"].ToString()))
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.ContentEncoding = HttpContext.Current.Request.ContentEncoding;
            HttpContext.Current.Response.AppendHeader("dv_ajax", "1");
            string s = "{message:\"\", goUrl:\"" + mUrl + "\"};";
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }
        HttpContext.Current.Response.Write("<script>alert('" + mAlertText + "');parent.location.href='" + mUrl + "';</script>");
        HttpContext.Current.Response.End();
    }

 

    public static bool IsCompriseStr(string str, string stringarray, string strsplit)
    {
        if (stringarray == "" || stringarray == null)
            return false;

        str = str.ToLower();
        string[] stringArray = Utils.SplitString(stringarray.ToLower(), strsplit);
        for (int i = 0; i < stringArray.Length; i++)
        {
            //string t1 = str;
            //string t2 = stringArray[i];
            if (str.IndexOf(stringArray[i]) > -1)
            {
                return true;
            }
        }
        return false;
    }

    /// <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)
    {
        for (int i = 0; i < stringArray.Length; i++)
        {
            if (caseInsensetive)
            {
                if (strSearch.ToLower() == stringArray[i].ToLower())
                {
                    return i;
                }
            }
            else
            {
                if (strSearch == stringArray[i])
                {
                    return i;
                }
            }

        }
        return -1;
    }


    /// <summary>
    /// 判断指定字符串在指定字符串数组中的位置
    /// </summary>
    /// <param name="strSearch">字符串</param>
    /// <param name="stringArray">字符串数组</param>
    /// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>  
    public static int GetInArrayID(string strSearch, string[] stringArray)
    {
        return GetInArrayID(strSearch, stringArray, true);
    }

    /// <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)
    {
        return GetInArrayID(strSearch, stringArray, caseInsensetive) >= 0;
    }

    /// <summary>
    /// 判断指定字符串是否属于指定字符串数组中的一个元素
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="stringarray">字符串数组</param>
    /// <returns>判断结果</returns>
    public static bool InArray(string str, string[] stringarray)
    {
        return InArray(str, stringarray, false);
    }

    /// <summary>
    /// 判断指定字符串是否属于指定字符串数组中的一个元素
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="stringarray">内部以逗号分割单词的字符串</param>
    /// <returns>判断结果</returns>
    public static bool InArray(string str, string stringarray)
    {
        return InArray(str, SplitString(stringarray, ","), false);
    }

    /// <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)
    {
        return InArray(str, SplitString(stringarray, strsplit), false);
    }

    /// <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)
    {
        return InArray(str, SplitString(stringarray, strsplit), caseInsensetive);
    }

    /// <summary>
    /// string型转换为int型
    /// </summary>
    /// <param name="strValue">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的int类型结果</returns>
    public static int StrToInt(object strValue, int defValue)
    {
        if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))
        {
            return defValue;
        }

        string val = strValue.ToString();
        string firstletter = val[0].ToString();

        if (val.Length == 10 && IsNumber(firstletter) && int.Parse(firstletter) > 1)
        {
            return defValue;
        }
        else if (val.Length == 10 && !IsNumber(firstletter))
        {
            return defValue;
        }


        int intValue = defValue;
        if (strValue != null)
        {
            bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());
            if (IsInt)
            {
                intValue = Convert.ToInt32(strValue);
            }
        }

        return intValue;
    }

    /// <summary>
    /// string型转换为float型
    /// </summary>
    /// <param name="strValue">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的int类型结果</returns>
    public static float StrToFloat(object strValue, float defValue)
    {
        if ((strValue == null) || (strValue.ToString().Length > 10))
        {
            return defValue;
        }

        float intValue = defValue;
        if (strValue != null)
        {
            bool IsFloat = new Regex(@"^([-]|[0-9])[0-9]*(\.\w*)?$").IsMatch(strValue.ToString());
            if (IsFloat)
            {
                intValue = Convert.ToSingle(strValue);
            }
        }
        return intValue;
    }

 

    /// <summary>
    /// 判断给定的字符串(strNumber)是否是数值型
    /// </summary>
    /// <param name="strNumber">要确认的字符串</param>
    /// <returns>是则返加true 不是则返回 false</returns>
    public static bool IsNumber(string strNumber)
    {
        return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
    }


    /// <summary>
    /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
    /// </summary>
    /// <param name="strNumber">要确认的字符串数组</param>
    /// <returns>是则返加true 不是则返回 false</returns>
    public static bool IsNumberArray(string[] strNumber)
    {
        if (strNumber == null)
        {
            return false;
        }
        if (strNumber.Length < 1)
        {
            return false;
        }
        foreach (string id in strNumber)
        {
            if (!IsNumber(id))
            {
                return false;
            }
        }
        return true;

    }


    /// <summary>
    /// 删除字符串尾部的回车/换行/空格
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string RTrim(string str)
    {
        for (int i = str.Length; i >= 0; i--)
        {
            if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
            {
                str.Remove(i, 1);
            }
        }
        return str;
    }


    /// <summary>
    /// 清除给定字符串中的回车及换行符
    /// </summary>
    /// <param name="str">要清除的字符串</param>
    /// <returns>清除后返回的字符串</returns>
    public static string ClearBR(string str)
    {
        Regex r = null;
        Match m = null;

        r = new Regex(@"(\r\n)", RegexOptions.IgnoreCase);
        for (m = r.Match(str); m.Success; m = m.NextMatch())
        {
            str = str.Replace(m.Groups[0].ToString(), "");
        }


        return str;
    }
    /// <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)
    {
        if (startIndex >= 0)
        {
            if (length < 0)
            {
                length = length * -1;
                if (startIndex - length < 0)
                {
                    length = startIndex;
                    startIndex = 0;
                }
                else
                {
                    startIndex = startIndex - length;
                }
            }


            if (startIndex > str.Length)
            {
                return "";
            }


        }
        else
        {
            if (length < 0)
            {
                return "";
            }
            else
            {
                if (length + startIndex > 0)
                {
                    length = length + startIndex;
                    startIndex = 0;
                }
                else
                {
                    return "";
                }
            }
        }

        if (str.Length - startIndex < length)
        {
            length = str.Length - startIndex;
        }

        try
        {
            return str.Substring(startIndex, length);
        }
        catch
        {
            return str;
        }
    }

    /// <summary>
    /// 从字符串的指定位置开始截取到字符串结尾的了符串
    /// </summary>
    /// <param name="str">原字符串</param>
    /// <param name="startIndex">子字符串的起始位置</param>
    /// <returns>子字符串</returns>
    public static string CutString(string str, int startIndex)
    {
        return CutString(str, startIndex, str.Length);
    }

 

    /// <summary>
    /// 获得当前绝对路径
    /// </summary>
    /// <param name="strPath">指定的路径</param>
    /// <returns>绝对路径</returns>
    public static string GetMapPath(string strPath)
    {
        if (HttpContext.Current != null)
        {
            return HttpContext.Current.Server.MapPath(strPath);
        }
        else //非web程序引用
        {
            return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }

 

    /// <summary>
    /// 返回文件是否存在
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <returns>是否存在</returns>
    public static bool FileExists(string filename)
    {
        return System.IO.File.Exists(filename);
    }

 

    /// <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)
    {
        Stream iStream = null;

        // 缓冲区为10k
        byte[] buffer = new Byte[10000];

        // 文件长度
        int length;

        // 需要读的数据长度
        long dataToRead;

        try
        {
            // 打开文件
            iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);


            // 需要读的数据长度
            dataToRead = iStream.Length;

            HttpContext.Current.Response.ContentType = filetype;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Utils.UrlEncode(filename.Trim()).Replace("+", " "));

            while (dataToRead > 0)
            {
                // 检查客户端是否还处于连接状态
                if (HttpContext.Current.Response.IsClientConnected)
                {
                    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
                {
                    // 如果不再连接则跳出死循环
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("Error : " + ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                // 关闭文件
                iStream.Close();
            }
        }
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 判断文件名是否为浏览器可以直接显示的图片文件名
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <returns>是否可以直接显示</returns>
    public static bool IsImgFilename(string filename)
    {
        filename = filename.Trim();
        if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
        {
            return false;
        }
        string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
        return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
    }


    /// <summary>
    /// int型转换为string型
    /// </summary>
    /// <returns>转换后的string类型结果</returns>
    public static string IntToStr(int intValue)
    {
        //
        return Convert.ToString(intValue);
    }
    /// <summary>
    /// MD5函数
    /// </summary>
    /// <param name="str">原始字符串</param>
    /// <returns>MD5结果</returns>
    public static string MD5(string str)
    {
        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;
    }

    /// <summary>
    /// SHA256函数
    /// </summary>
    /// /// <param name="str">原始字符串</param>
    /// <returns>SHA256结果</returns>
    public static string SHA256(string str)
    {
        byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
        SHA256Managed Sha256 = new SHA256Managed();
        byte[] Result = Sha256.ComputeHash(SHA256Data);
        return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
    }

 

    /// <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)
    {
        return GetSubString(p_SrcString, 0, p_Length, p_TailString);
    }


    /// <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)
    {


        string myResult = p_SrcString;

        //当是日文或韩文时(注:中文的范围:\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]+"))
        {
            //当截取的起始位置超出字段串长度时
            if (p_StartIndex >= p_SrcString.Length)
            {
                return "";
            }
            else
            {
                return p_SrcString.Substring(p_StartIndex,
                                               ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
            }
        }


        if (p_Length >= 0)
        {
            byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);

            //当字符串长度大于起始位置
            if (bsSrcString.Length > p_StartIndex)
            {
                int p_EndIndex = bsSrcString.Length;

                //当要截取的长度在字符串的有效长度范围内
                if (bsSrcString.Length > (p_StartIndex + p_Length))
                {
                    p_EndIndex = p_Length + p_StartIndex;
                }
                else
                {   //当不在有效范围内时,只取到字符串的结尾

                    p_Length = bsSrcString.Length - p_StartIndex;
                    p_TailString = "";
                }

 

                int nRealLength = p_Length;
                int[] anResultFlag = new int[p_Length];
                byte[] bsResult = null;

                int nFlag = 0;
                for (int i = p_StartIndex; i < p_EndIndex; i++)
                {

                    if (bsSrcString[i] > 127)
                    {
                        nFlag++;
                        if (nFlag == 3)
                        {
                            nFlag = 1;
                        }
                    }
                    else
                    {
                        nFlag = 0;
                    }

                    anResultFlag[i] = nFlag;
                }

                if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                {
                    nRealLength = p_Length + 1;
                }

                bsResult = new byte[nRealLength];

                Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);

                myResult = Encoding.Default.GetString(bsResult);

                myResult = myResult + p_TailString;
            }
        }

        return myResult;
    }


    /// <summary>
    /// 自定义的替换字符串函数
    /// </summary>
    public static string ReplaceString(string SourceString, string SearchString, string ReplaceString, bool IsCaseInsensetive)
    {
        return Regex.Replace(SourceString, Regex.Escape(SearchString), ReplaceString, IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
    }

    /// <summary>
    /// 生成指定数量的html空格符号
    /// </summary>
    public static string Spaces(int nSpaces)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nSpaces; i++)
        {
            sb.Append(" &nbsp;&nbsp;");
        }
        return sb.ToString();
    }

    /// <summary>
    /// 检测是否符合email格式
    /// </summary>
    /// <param name="strEmail">要判断的email字符串</param>
    /// <returns>判断结果</returns>
    public static bool IsValidEmail(string strEmail)
    {
        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})(\]?)$");
    }
 

    public static string GetEmailHostName(string strEmail)
    {
        if (strEmail.IndexOf("@") < 0)
        {
            return "";
        }
        return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();
    }

    /// <summary>
    /// 判断是否为base64字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool IsBase64String(string str)
    {
        //A-Z, a-z, 0-9, +, /, =
        return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
    }
    /// <summary>
    /// 检测是否有Sql危险字符
    /// </summary>
    /// <param name="str">要判断字符串</param>
    /// <returns>判断结果</returns>
    public static bool IsSafeSqlString(string str)
    {

        return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
    }

    /// <summary>
    /// 检测是否有危险的可能用于链接的字符串
    /// </summary>
    /// <param name="str">要判断字符串</param>
    /// <returns>判断结果</returns>
    public static bool IsSafeUserInfoString(string str)
    {
        return !Regex.IsMatch(str, @"/^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|$guestexp/is");
    }

    /// <summary>
    /// 清理字符串
    /// </summary>
    public static string CleanInput(string strIn)
    {
        return Regex.Replace(strIn.Trim(), @"[^\w\.@-]", "");
    }

    /// <summary>
    /// 返回URL中结尾的文件名
    /// </summary>  
    public static string GetFilename(string url)
    {
        if (url == null)
        {
            return "";
        }
        string[] strs1 = url.Split(new char[] { '/' });
        return strs1[strs1.Length - 1].Split(new char[] { '?' })[0];
    }

    /// <summary>
    /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
    /// </summary> 
    public static string[] Monthes
    {
        get
        {
            return new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        }
    }

    /// <summary>
    /// 替换回车换行符为html换行符
    /// </summary>
    public static string StrFormat(string str)
    {
        string str2;

        if (str == null)
        {
            str2 = "";
        }
        else
        {
            str = str.Replace("\r\n", "<br />");
            str = str.Replace("\n", "<br />");
            str2 = str;
        }
        return str2;
    }

    /// <summary>
    /// 返回标准日期格式string
    /// </summary>
    public static string GetDate()
    {
        return DateTime.Now.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 返回指定日期格式
    /// </summary>
    public static string GetDate(string datetimestr, string replacestr)
    {
        if (datetimestr == null)
        {
            return replacestr;
        }

        if (datetimestr.Equals(""))
        {
            return replacestr;
        }

        try
        {
            datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
        }
        catch
        {
            return replacestr;
        }
        return datetimestr;

    }


    /// <summary>
    /// 返回标准时间格式string
    /// </summary>
    public static string GetTime()
    {
        return DateTime.Now.ToString("HH:mm:ss");
    }

    /// <summary>
    /// 返回标准时间格式string
    /// </summary>
    public static string GetDateTime()
    {
        return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// 返回相对于当前时间的相对天数
    /// </summary>
    public static string GetDateTime(int relativeday)
    {
        return DateTime.Now.AddDays(relativeday).ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// 返回标准时间格式string
    /// </summary>
    public static string GetDateTimeF()
    {
        return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
    }

    /// <summary>
    /// 返回标准时间
    /// </sumary>
    public static string GetStandardDateTime(string fDateTime, string formatStr)
    {
        DateTime s = Convert.ToDateTime(fDateTime);
        return s.ToString(formatStr);
    }

    /// <summary>
    /// 返回标准时间 yyyy-MM-dd HH:mm:ss
    /// </sumary>
    public static string GetStandardDateTime(string fDateTime)
    {
        return GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static bool IsTime(string timeval)
    {
        return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
    }


    public static string GetRealIP()
    {
        string ip = DNTRequest.GetIP();

        return ip;
    }

    /// <summary>
    /// 改正sql语句中的转义字符
    /// </summary>
    public static string mashSQL(string str)
    {
        string str2;

        if (str == null)
        {
            str2 = "";
        }
        else
        {
            str = str.Replace("\'", "'");
            str2 = str;
        }
        return str2;
    }

    /// <summary>
    /// 替换sql语句中的有问题符号
    /// </summary>
    public static string ChkSQL(string str)
    {
        string str2;

        if (str == null)
        {
            str2 = "";
        }
        else
        {
            str = str.Replace("'", "'");
            str2 = str;
        }
        return str2;
    }


    /// <summary>
    /// 转换为静态html
    /// </summary>
    public void transHtml(string path, string outpath)
    {
        Page page = new Page();
        StringWriter writer = new StringWriter();
        page.Server.Execute(path, writer);
        FileStream fs;
        if (File.Exists(page.Server.MapPath("") + "\\" + outpath))
        {
            File.Delete(page.Server.MapPath("") + "\\" + outpath);
            fs = File.Create(page.Server.MapPath("") + "\\" + outpath);
        }
        else
        {
            fs = File.Create(page.Server.MapPath("") + "\\" + outpath);
        }
        byte[] bt = Encoding.Default.GetBytes(writer.ToString());
        fs.Write(bt, 0, bt.Length);
        fs.Close();
    }


    /// <summary>
    /// 转换为简体中文
    /// </summary>
    public static string ToSChinese(string str)
    {
        return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);
    }

    /// <summary>
    /// 转换为繁体中文
    /// </summary>
    public static string ToTChinese(string str)
    {
        return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);
    }

    /// <summary>
    /// 分割字符串
    /// </summary>
    public static string[] SplitString(string strContent, string strSplit)
    {
        if (strContent.IndexOf(strSplit) < 0)
        {
            string[] tmp = { strContent };
            return tmp;
        }
        return Regex.Split(strContent, @strSplit.Replace(".", @"\."), RegexOptions.IgnoreCase);
    }

    /// <summary>
    /// 替换html字符
    /// </summary>
    public static string EncodeHtml(string strHtml)
    {
        if (strHtml != "")
        {
            strHtml = strHtml.Replace(",", "&def");
            strHtml = strHtml.Replace("'", "&dot");
            strHtml = strHtml.Replace(";", "&dec");
            return strHtml;
        }
        return "";
    }

 

    public static string ClearHtml(string strHtml)
    {
        if (strHtml != "")
        {
            Regex r = null;
            Match m = null;

            r = new Regex(@"<\/?[^>]*>", RegexOptions.IgnoreCase);
            for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
            {
                strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
            }
        }
        return strHtml;
    }


    /// <summary>
    /// 进行指定的替换(脏字过滤)
    /// </summary>
    public static string StrFilter(string str, string bantext)
    {
        string text1 = "";
        string text2 = "";
        string[] textArray1 = SplitString(bantext, "\r\n");
        for (int num1 = 0; num1 < textArray1.Length; num1++)
        {
            text1 = textArray1[num1].Substring(0, textArray1[num1].IndexOf("="));
            text2 = textArray1[num1].Substring(textArray1[num1].IndexOf("=") + 1);
            str = str.Replace(text1, text2);
        }
        return str;
    }


    /// <summary>
    /// 获得伪静态页码显示链接
    /// </summary>
    /// <param name="curPage">当前页数</param>
    /// <param name="countPage">总页数</param>
    /// <param name="url">超级链接地址</param>
    /// <param name="extendPage">周边页码显示个数上限</param>
    /// <returns>页码html</returns>
    public static string GetStaticPageNumbers(int curPage, int countPage, string url, string expname, int extendPage)
    {
        int startPage = 1;
        int endPage = 1;

        string t1 = "<a href=\"" + url + "-1" + expname + "\">&laquo;</a>";
        string t2 = "<a href=\"" + url + "-" + countPage + expname + "\">&raquo;</a>";

        if (countPage < 1) countPage = 1;
        if (extendPage < 3) extendPage = 2;

        if (countPage > extendPage)
        {
            if (curPage - (extendPage / 2) > 0)
            {
                if (curPage + (extendPage / 2) < countPage)
                {
                    startPage = curPage - (extendPage / 2);
                    endPage = startPage + extendPage - 1;
                }
                else
                {
                    endPage = countPage;
                    startPage = endPage - extendPage + 1;
                    t2 = "";
                }
            }
            else
            {
                endPage = extendPage;
                t1 = "";
            }
        }
        else
        {
            startPage = 1;
            endPage = countPage;
            t1 = "";
            t2 = "";
        }

        StringBuilder s = new StringBuilder("");

        s.Append(t1);
        for (int i = startPage; i <= endPage; i++)
        {
            if (i == curPage)
            {
                s.Append("<span>");
                s.Append(i);
                s.Append("</span>");
            }
            else
            {
                s.Append("<a href=\"");
                s.Append(url);
                s.Append("-");
                s.Append(i);
                s.Append(expname);
                s.Append("\">");
                s.Append(i);
                s.Append("</a>");
            }
        }
        s.Append(t2);

        return s.ToString();
    }


    /// <summary>
    /// 获得帖子的伪静态页码显示链接
    /// </summary>
    /// <param name="expname"></param>
    /// <param name="countPage">总页数</param>
    /// <param name="url">超级链接地址</param>
    /// <param name="extendPage">周边页码显示个数上限</param>
    /// <returns>页码html</returns>
    public static string GetPostPageNumbers(int countPage, string url, string expname, int extendPage)
    {
        int startPage = 1;
        int endPage = 1;
        int curPage = 1;

        string t1 = "<a href=\"" + url + "-1" + expname + "\">&laquo;</a>";
        string t2 = "<a href=\"" + url + "-" + countPage + expname + "\">&raquo;</a>";

        if (countPage < 1) countPage = 1;
        if (extendPage < 3) extendPage = 2;

        if (countPage > extendPage)
        {
            if (curPage - (extendPage / 2) > 0)
            {
                if (curPage + (extendPage / 2) < countPage)
                {
                    startPage = curPage - (extendPage / 2);
                    endPage = startPage + extendPage - 1;
                }
                else
                {
                    endPage = countPage;
                    startPage = endPage - extendPage + 1;
                    t2 = "";
                }
            }
            else
            {
                endPage = extendPage;
                t1 = "";
            }
        }
        else
        {
            startPage = 1;
            endPage = countPage;
            t1 = "";
            t2 = "";
        }

        StringBuilder s = new StringBuilder("");

        s.Append(t1);
        for (int i = startPage; i <= endPage; i++)
        {
            s.Append("<a href=\"");
            s.Append(url);
            s.Append("-");
            s.Append(i);
            s.Append(expname);
            s.Append("\">");
            s.Append(i);
            s.Append("</a>");
        }
        s.Append(t2);

        return s.ToString();
    }

 

    /// <summary>
    /// 获得页码显示链接
    /// </summary>
    /// <param name="curPage">当前页数</param>
    /// <param name="countPage">总页数</param>
    /// <param name="url">超级链接地址</param>
    /// <param name="extendPage">周边页码显示个数上限</param>
    /// <returns>页码html</returns>
    public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage)
    {
        return GetPageNumbers(curPage, countPage, url, extendPage, "page");
    }

    /// <summary>
    /// 获得页码显示链接
    /// </summary>
    /// <param name="curPage">当前页数</param>
    /// <param name="countPage">总页数</param>
    /// <param name="url">超级链接地址</param>
    /// <param name="extendPage">周边页码显示个数上限</param>
    /// <param name="pagetag">页码标记</param>
    /// <returns>页码html</returns>
    public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage, string pagetag)
    {
        return GetPageNumbers(curPage, countPage, url, extendPage, pagetag, null);
    }

    /// <summary>
    /// 获得页码显示链接
    /// </summary>
    /// <param name="curPage">当前页数</param>
    /// <param name="countPage">总页数</param>
    /// <param name="url">超级链接地址</param>
    /// <param name="extendPage">周边页码显示个数上限</param>
    /// <param name="pagetag">页码标记</param>
    /// <param name="anchor">锚点</param>
    /// <returns>页码html</returns>
    public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage, string pagetag, string anchor)
    {
        if (pagetag == "")
            pagetag = "page";
        int startPage = 1;
        int endPage = 1;

        if (url.IndexOf("?") > 0)
        {
            url = url + "&";
        }
        else
        {
            url = url + "?";
        }

        string t1 = "<a href=\"" + url + "&" + pagetag + "=1";
        string t2 = "<a href=\"" + url + "&" + pagetag + "=" + countPage;
        if (anchor != null)
        {
            t1 += anchor;
            t2 += anchor;
        }
        t1 += "\">&laquo;</a>";
        t2 += "\">&raquo;</a>";

        if (countPage < 1)
            countPage = 1;
        if (extendPage < 3)
            extendPage = 2;

        if (countPage > extendPage)
        {
            if (curPage - (extendPage / 2) > 0)
            {
                if (curPage + (extendPage / 2) < countPage)
                {
                    startPage = curPage - (extendPage / 2);
                    endPage = startPage + extendPage - 1;
                }
                else
                {
                    endPage = countPage;
                    startPage = endPage - extendPage + 1;
                    t2 = "";
                }
            }
            else
            {
                endPage = extendPage;
                t1 = "";
            }
        }
        else
        {
            startPage = 1;
            endPage = countPage;
            t1 = "";
            t2 = "";
        }

        StringBuilder s = new StringBuilder("");

        s.Append(t1);
        for (int i = startPage; i <= endPage; i++)
        {
            if (i == curPage)
            {
                s.Append("<span>");
                s.Append(i);
                s.Append("</span>");
            }
            else
            {
                s.Append("<a href=\"");
                s.Append(url);
                s.Append(pagetag);
                s.Append("=");
                s.Append(i);
                if (anchor != null)
                {
                    s.Append(anchor);
                }
                s.Append("\">");
                s.Append(i);
                s.Append("</a>");
            }
        }
        s.Append(t2);

        return s.ToString();
    }

    /// <summary>
    /// 返回 HTML 字符串的编码结果
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>编码结果</returns>
    public static string HtmlEncode(string str)
    {
        return HttpUtility.HtmlEncode(str);
    }

    /// <summary>
    /// 返回 HTML 字符串的解码结果
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>解码结果</returns>
    public static string HtmlDecode(string str)
    {
        return HttpUtility.HtmlDecode(str);
    }

    /// <summary>
    /// 返回 URL 字符串的编码结果
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>编码结果</returns>
    public static string UrlEncode(string str)
    {
        return HttpUtility.UrlEncode(str);
    }

    /// <summary>
    /// 返回 URL 字符串的编码结果
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>解码结果</returns>
    public static string UrlDecode(string str)
    {
        return HttpUtility.UrlDecode(str);
    }


    /// <summary>
    /// 返回指定目录下的非 UTF8 字符集文件
    /// </summary>
    /// <param name="Path">路径</param>
    /// <returns>文件名的字符串数组</returns>
    public static string[] FindNoUTF8File(string Path)
    {
        //System.IO.StreamReader reader = null;
        StringBuilder filelist = new StringBuilder();
        DirectoryInfo Folder = new DirectoryInfo(Path);
        //System.IO.DirectoryInfo[] subFolders = Folder.GetDirectories();
        /*
        for (int i=0;i<subFolders.Length;i++)
        {
            FindNoUTF8File(subFolders[i].FullName);
        }
        */
        FileInfo[] subFiles = Folder.GetFiles();
        for (int j = 0; j < subFiles.Length; j++)
        {
            if (subFiles[j].Extension.ToLower().Equals(".htm"))
            {
                FileStream fs = new FileStream(subFiles[j].FullName, FileMode.Open, FileAccess.Read);
                bool bUtf8 = IsUTF8(fs);
                fs.Close();
                if (!bUtf8)
                {
                    filelist.Append(subFiles[j].FullName);
                    filelist.Append("\r\n");
                }
            }
        }
        return Utils.SplitString(filelist.ToString(), "\r\n");

    }

    //0000 0000-0000 007F - 0xxxxxxx  (ascii converts to 1 octet!)
    //0000 0080-0000 07FF - 110xxxxx 10xxxxxx    ( 2 octet format)
    //0000 0800-0000 FFFF - 1110xxxx 10xxxxxx 10xxxxxx (3 octet format)

    /// <summary>
    /// 判断文件流是否为UTF8字符集
    /// </summary>
    /// <param name="sbInputStream">文件流</param>
    /// <returns>判断结果</returns>
    private static bool IsUTF8(FileStream sbInputStream)
    {
        int i;
        byte cOctets;  // octets to go in this UTF-8 encoded character
        byte chr;
        bool bAllAscii = true;
        long iLen = sbInputStream.Length;

        cOctets = 0;
        for (i = 0; i < iLen; i++)
        {
            chr = (byte)sbInputStream.ReadByte();

            if ((chr & 0x80) != 0) bAllAscii = false;

            if (cOctets == 0)
            {
                if (chr >= 0x80)
                {
                    do
                    {
                        chr <<= 1;
                        cOctets++;
                    }
                    while ((chr & 0x80) != 0);

                    cOctets--;
                    if (cOctets == 0) return false;
                }
            }
            else
            {
                if ((chr & 0xC0) != 0x80)
                {
                    return false;
                }
                cOctets--;
            }
        }

        if (cOctets > 0)
        {
            return false;
        }

        if (bAllAscii)
        {
            return false;
        }

        return true;

    }

    /// <summary>
    /// 格式化字节数字符串
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string FormatBytesStr(int bytes)
    {
        if (bytes > 1073741824)
        {
            return ((double)(bytes / 1073741824)).ToString("0") + "G";
        }
        if (bytes > 1048576)
        {
            return ((double)(bytes / 1048576)).ToString("0") + "M";
        }
        if (bytes > 1024)
        {
            return ((double)(bytes / 1024)).ToString("0") + "K";
        }
        return bytes.ToString() + "Bytes";
    }

    /// <summary>
    /// 将long型数值转换为Int32类型
    /// </summary>
    /// <param name="objNum"></param>
    /// <returns></returns>
    public static int SafeInt32(object objNum)
    {
        if (objNum == null)
        {
            return 0;
        }
        string strNum = objNum.ToString();
        if (IsNumber(strNum))
        {

            if (strNum.ToString().Length > 9)
            {
                return int.MaxValue;
            }
            return Int32.Parse(strNum);
        }
        else
        {
            return 0;
        }
    }

    /// <summary>
    /// 返回相差的秒数
    /// </summary>
    /// <param name="Time"></param>
    /// <param name="Sec"></param>
    /// <returns></returns>
    public static int StrDateDiffSeconds(string Time, int Sec)
    {
        TimeSpan ts = DateTime.Now - DateTime.Parse(Time).AddSeconds(Sec);
        if (ts.TotalSeconds > int.MaxValue)
        {
            return int.MaxValue;
        }
        else if (ts.TotalSeconds < int.MinValue)
        {
            return int.MinValue;
        }
        return (int)ts.TotalSeconds;
    }

    /// <summary>
    /// 返回相差的分钟数
    /// </summary>
    /// <param name="time"></param>
    /// <param name="minutes"></param>
    /// <returns></returns>
    public static int StrDateDiffMinutes(string time, int minutes)
    {
        if (time == "" || time == null)
            return 1;
        TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddMinutes(minutes);
        if (ts.TotalMinutes > int.MaxValue)
        {
            return int.MaxValue;
        }
        else if (ts.TotalMinutes < int.MinValue)
        {
            return int.MinValue;
        }
        return (int)ts.TotalMinutes;
    }

    /// <summary>
    /// 返回相差的小时数
    /// </summary>
    /// <param name="time"></param>
    /// <param name="hours"></param>
    /// <returns></returns>
    public static int StrDateDiffHours(string time, int hours)
    {
        if (time == "" || time == null)
            return 1;
        TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddHours(hours);
        if (ts.TotalHours > int.MaxValue)
        {
            return int.MaxValue;
        }
        else if (ts.TotalHours < int.MinValue)
        {
            return int.MinValue;
        }
        return (int)ts.TotalHours;
    }

    /// <summary>
    /// 建立文件夹
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static bool CreateDir(string name)
    {
        return Utils.MakeSureDirectoryPathExists(name);
    }

    /// <summary>
    /// 为脚本替换特殊字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ReplaceStrToScript(string str)
    {
        str = str.Replace("\\", "\\\\");
        str = str.Replace("'", "\\'");
        str = str.Replace("\"", "\\\"");
        return str;
    }

    /// <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?)$");
    }

    /// <summary>
    /// 返回指定IP是否在指定的IP数组所限定的范围内, IP数组内的IP地址可以使用*表示该IP段任意, 例如192.168.1.*
    /// </summary>
    /// <param name="ip"></param>
    /// <param name="iparray"></param>
    /// <returns></returns>
    public static bool InIPArray(string ip, string[] iparray)
    {

        string[] userip = Utils.SplitString(ip, @".");
        for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++)
        {
            string[] tmpip = Utils.SplitString(iparray[ipIndex], @".");
            int r = 0;
            for (int i = 0; i < tmpip.Length; i++)
            {
                if (tmpip[i] == "*")
                {
                    return true;
                }

                if (userip.Length > i)
                {
                    if (tmpip[i] == userip[i])
                    {
                        r++;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }

            }
            if (r == 4)
            {
                return true;
            }


        }
        return false;

    }

    /// <summary>
    /// 获得Assembly版本号
    /// </summary>
    /// <returns></returns>
    public static string GetAssemblyVersion()
    {
        Assembly myAssembly = Assembly.GetExecutingAssembly();
        FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo(myAssembly.Location);
        return string.Format("{0}.{1}.{2}", myFileVersion.FileMajorPart, myFileVersion.FileMinorPart, myFileVersion.FileBuildPart);
    }

    /// <summary>
    /// 获得Assembly产品名称
    /// </summary>
    /// <returns></returns>
    public static string GetAssemblyProductName()
    {
        Assembly myAssembly = Assembly.GetExecutingAssembly();
        FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo(myAssembly.Location);
        return myFileVersion.ProductName;
    }

    /// <summary>
    /// 获得Assembly产品版权
    /// </summary>
    /// <returns></returns>
    public static string GetAssemblyCopyright()
    {
        Assembly myAssembly = Assembly.GetExecutingAssembly();
        FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo(myAssembly.Location);
        return myFileVersion.LegalCopyright;
    }
    /// <summary>
    /// 创建目录
    /// </summary>
    /// <param name="name">名称</param>
    /// <returns>创建是否成功</returns>
    [DllImport("dbgHelp", SetLastError = true)]
    private static extern bool MakeSureDirectoryPathExists(string name);


    /// <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 = strValue;
        HttpContext.Current.Response.AppendCookie(cookie);

    }
    /// <summary>
    /// 写cookie值
    /// </summary>
    /// <param name="strName">名称</param>
    /// <param name="strValue">值</param>
    public static void WriteCookie(string strName, string strValue, int expires)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
        if (cookie == null)
        {
            cookie = new HttpCookie(strName);
        }
        cookie.Value = strValue;
        cookie.Expires = DateTime.Now.AddDays(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 HttpContext.Current.Request.Cookies[strName].Value.ToString();
        }

        return "";
    }

    /// <summary>
    /// 得到论坛的真实路径
    /// </summary>
    /// <returns></returns>
    public static string GetTrueForumPath()
    {
        string forumPath = HttpContext.Current.Request.Path;
        if (forumPath.LastIndexOf("/") != forumPath.IndexOf("/"))
        {
            forumPath = forumPath.Substring(forumPath.IndexOf("/"), forumPath.LastIndexOf("/") + 1);
        }
        else
        {
            forumPath = "/";
        }
        return forumPath;

    }

    /// <summary>
    /// 判断字符串是否是yy-mm-dd字符串
    /// </summary>
    /// <param name="str">待判断字符串</param>
    /// <returns>判断结果</returns>
    public static bool IsDateString(string str)
    {
        return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
    }

    /// <summary>
    /// 移除Html标记
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static string RemoveHtml(string content)
    {
        string regexstr = @"<[^>]*>";
        return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
    }

    /// <summary>
    /// 上傳文件,不限制大小和類型並上傳到默認UploadFile目錄
    /// </summary>
    /// <param name="postedFile">上傳文件控件</param>
    /// <returns>上傳後文件的名稱</returns>
    public static string UploadFile(System.Web.UI.WebControls.FileUpload postedFile)
    {
        return UploadFile(postedFile, "", 0, "");
    }


    /// <summary>
    /// 上傳文件,不限制大小和類型並上傳到自定義路徑
    /// </summary>
    /// <param name="postedFile"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string UploadFile(System.Web.UI.WebControls.FileUpload postedFile, string path)
    {
        return UploadFile(postedFile, path, 0, "");
    }
  

    public static string UploadFile(System.Web.UI.WebControls.FileUpload postedFile, string path, int size, string filetype)
    {
        try
        {
            string strAllPath = postedFile.PostedFile.FileName;
            //上傳文件的完整原始物理路徑               
            string strAllName = System.IO.Path.GetFileName(postedFile.PostedFile.FileName);
            //原文件名(包括格式)
            string strtype = System.IO.Path.GetExtension(postedFile.PostedFile.FileName);
            //擴展名
            string strName = System.IO.Path.GetFileNameWithoutExtension(postedFile.PostedFile.FileName);
            //文件名(不包含格式)

            string strSaveName = Guid.NewGuid() + strtype;

            if (string.IsNullOrEmpty(strName))
            {
                return "";
            }

            //上傳後的文件名稱
            if (path == "")
            {
                path = HttpContext.Current.Request.PhysicalApplicationPath + "\\ProductImages\\" + DateTime.Now.Date.ToString("yyyy/MM/dd") + "\\";
                if (System.IO.Directory.Exists(path) == false)
                {
                    System.IO.Directory.CreateDirectory(path);
                    //如果沒就創建一個目錄;
                }
            }
            string strSavePath = path + strSaveName;
            //爲文件獲取保存路徑
            int fileSize = postedFile.PostedFile.ContentLength / 1024;


            //是否限制文件類型
            if (filetype.Length > 0)
            {
                if (filetype.ToLower().Equals("default"))
                {
                    if (strtype.ToLower() != ".jpg" && strtype.ToLower() != ".png" && strtype.ToLower() != ".gif")
                    {
                        return "1";
                    }
                }
                else
                {
                    string[] uploadType = filetype.Split('|');
                    foreach (string type in uploadType)
                    {
                        if (strtype.ToLower() != type)
                        {
                            return "1";
                        }
                    }
                }
            }
            //是否限制大小
            if (size > 0)
            {
                if (fileSize <= size)
                {
                    postedFile.PostedFile.SaveAs(strSavePath);
                    //保存上傳的文件到指定的路徑
                    //Page.RegisterClientScriptBlock("", "<script>alert('上傳成功!');</script>");
                    return strSaveName;
                }
                else
                {
                    //上傳的文件大小超出
                    return "2";
                }
            }
            else
            {
                postedFile.PostedFile.SaveAs(strSavePath);
                //保存上傳的文件到指定的路徑
                return DateTime.Now.Date.ToString("yyyy/MM/dd") + "\\" + strSaveName;
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    /// <summary>
    /// 生成縮略圖
    /// </summary>
    /// <param name="originalImagePath">源圖路徑(物理路徑)</param>
    /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
    /// <param name="width">縮略圖寬度</param>
    /// <param name="height">縮略圖高度</param>
    /// <param name="mode">生成縮略圖的方式</param>   
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
        int towidth = width;
        int toheight = height;
        int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;
        switch (mode)
        {
            case "HW":
                //指定高寬縮放(可能變形)               
                break; // TODO: might not be correct. Was : Exit Select

              
            case "W":
                //指定寬,高按比例                   
                toheight = originalImage.Height * width / originalImage.Width;
                break; // TODO: might not be correct. Was : Exit Select

             
            case "H":
                //指定高,寬按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break; // TODO: might not be correct. Was : Exit Select

            
            case "Cut":
                //指定高寬裁減(不變形)               
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }

                break; // TODO: might not be correct. Was : Exit Select

              
            default:
                break; // TODO: might not be correct. Was : Exit Select

              
        }
        //新建一個bmp圖片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
        //新建一個畫板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //設置高質量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //設置高質量,低速度呈現平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空畫布並以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);
        //在指定位置並且按指定大小繪制原圖片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
        try
        {
            //以jpg格式保存縮略圖
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }
    //
    /// <summary>
    /// 在圖片上增加文字水印
    /// </summary>
    /// <param name="Path">原服務器圖片路徑</param>
    /// <param name="Path_sy">生成的帶文字水印的圖片路徑</param>
    /// <param name="addText">生成的文字</param>
    public static void AddWater(string Path, string Path_sy, string addText)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(image, 0, 0, image.Width, image.Height);
        System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
        g.DrawString(addText, f, b, 35, 35);
        g.Dispose();
        image.Save(Path_sy);
        image.Dispose();
    }
    //
    /// <summary>
    /// 在圖片上生成圖片水印
    /// </summary>
    /// <param name="Path">原服務器圖片路徑</param>
    /// <param name="Path_syp">生成的帶圖片水印的圖片路徑</param>
    /// <param name="Path_sypf">水印圖片路徑</param>
    public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        g.Dispose();
        image.Save(Path_syp);
        image.Dispose();
    }

 

    /// <summary>
    /// 保存上传的文件
    /// </summary>
    public static List<Model.KuanShi>  SaveRequestFilesP(int pid,int fn )
    {

        int fCount = HttpContext.Current.Request.Files.Count;

        List<Model.KuanShi> l = new List<Model.KuanShi>();
     
    

        Random random = new Random(unchecked((int)DateTime.Now.Ticks));


        int saveFileCount = 1;
        for (int i = 0; i < fCount; i++)
        {
          
            if (!HttpContext.Current.Request.Files[i].FileName.Equals(""))
            {
                Model.KuanShi attachmentinfo = new Model.KuanShi();
              
                #region
                string filename = Path.GetFileName(HttpContext.Current.Request.Files[i].FileName);
                string fileextname = Utils.CutString(filename, filename.LastIndexOf(".") + 1).ToLower();
                string filetype = HttpContext.Current.Request.Files[i].ContentType.ToLower();
                int filesize = HttpContext.Current.Request.Files[i].ContentLength;
                string newfilename = "";
                string newfilenemexiao="";

                if (saveFileCount > fn)
                {

                    // 判断 文件扩展名/文件大小/文件类型 是否符合要求
                    if (!(Utils.IsImgFilename(filename) && !filetype.StartsWith("image")))
                    {
                        string UploadDir =HttpContext.Current.Request.PhysicalApplicationPath+"productimages/";
                        StringBuilder savedir = new StringBuilder("");
                        //附件保存方式 0=按年/月/日存入不同目录 1=按年/月/日/论坛存入不同目录 2=按论坛存入不同目录 3=按文件类型存入不同目录

                        savedir.Append(DateTime.Now.ToString("yyyy"));
                        savedir.Append("-");
                        savedir.Append(DateTime.Now.ToString("MM"));
                        savedir.Append("-");
                        savedir.Append(DateTime.Now.ToString("dd"));
                        savedir.Append("/");

                        // 如果指定目录不存在则建立
                        if (!Directory.Exists(UploadDir + savedir.ToString()))
                        {
                            Utils.CreateDir(UploadDir + savedir.ToString());
                        }

                        newfilename = savedir.ToString() + (Environment.TickCount & int.MaxValue).ToString() + i.ToString() + random.Next(1000, 9999).ToString() + "." + fileextname;
                        newfilenemexiao = savedir.ToString() + (Environment.TickCount & int.MaxValue).ToString() + i.ToString() + random.Next(1000, 9999).ToString() + "xiao." + fileextname;

                        #region
                        try
                        {
                            // 如果是bmp jpg png图片类型
                            if ((fileextname == "bmp" || fileextname == "jpg" || fileextname == "jpeg" || fileextname == "png") && filetype.StartsWith("image"))
                            {


                                HttpContext.Current.Request.Files[i].SaveAs(UploadDir + newfilename);


                                MakeThumbnail(UploadDir + newfilename, UploadDir + newfilenemexiao, 80, 80, "Cut");

                                //生成縮量圖


                            }
                        }
                        catch
                        {
                            if (!(Utils.FileExists(UploadDir + newfilename)))
                            {
                                HttpContext.Current.Request.Files[i].SaveAs(UploadDir + newfilename);
                            }
                        }

                        #endregion
                    }

                    attachmentinfo.ChaoDaTu = newfilename;
                    attachmentinfo.PID = pid;
                    attachmentinfo.TuPianType = "1";
                    attachmentinfo.LuRuTime = DateAndTime.Now;
                    attachmentinfo.IFXianShi = "Y";
                    attachmentinfo.XiaoTu = newfilenemexiao;

                  
                    l.Add(attachmentinfo);
                }

                saveFileCount++;


                #endregion
            }


        }

        return l;
    }
      

}

 

 

原创粉丝点击