常用的.net公用类方法

来源:互联网 发布:淘宝店标图片清新 编辑:程序博客网 时间:2024/05/21 14:08

///   <summary>  
    ///   浏览某一目录下的所有文件信息并返回相关信息的数组  
    ///   </summary>  
    ///   <param   name="str">c:\test\test(路径)</param>  
    ///   <returns>0文件名,1创建时间,2扩展名,3文件大小</returns>  
    public static string[] GetFileInfo(string str)
    {

        str = HttpUtility.UrlDecode(str);
        string[] temp = new string[4];
        DirectoryInfo dir = new DirectoryInfo(str);
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            temp[0] += file.Name.ToString() + "|";   //文件名  
            temp[1] += file.CreationTime.ToString() + "|";   //文件日期  
            temp[2] += file.Extension.ToString() + "|";//扩展名  
            temp[3] += file.Length + "|";//文件长度  
        }
        return temp;
    }
    /// <summary>
    /// 过滤非法字符
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string filtRiskChar(string str) //过滤非法字符
    {
        string s = "";

        s = str.Replace("'", " ");
        s = s.Replace(";", " ");
        s = s.Replace("1=1", " ");
        s = s.Replace("|", " ");
        s = s.Replace("<", " ");
        s = s.Replace(">", " ");

        return s;
    }

/// <summary>
    /// 对实体类进行排序
    /// </summary>
    /// <typeparam name="T">实体类型,如:User</typeparam>
    /// <param name="list">实体类的数组</param>
    /// <param name="order">排序字段(必须为属性)</param>
    /// <param name="asc">是否按正序排序</param>
    public static void Sort<T>(object[] list, string order, bool asc)
    {
        Type type = typeof(T);
        PropertyInfo[] pros = type.GetProperties();
        PropertyInfo pro = pros[0];
        order = order.ToLower();
        for (int i = 0; i < pros.Length; i++)
        {
            if (pros[i].Name.ToLower().Equals(order))
            {
                pro = pros[i];
                break;
            }
        }
        object obj;
        int j, k = 1;
        bool done = false;
        int len = list.Length;
        while (k < len && !done)
        {
            done = true;
            for (j = 0; j < len - k; j++)
            {
                int b = pro.GetValue(list[j], null).ToString().CompareTo(pro.GetValue(list[j + 1], null).ToString());
                if ((asc && b > 0) || (!asc && b < 0))
                {
                    done = false;
                    obj = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = obj;
                }
            }
            k++;
        }
    }

/// <summary>
    /// 取得绝对路径
    /// </summary>
    /// <param name="strPath"></param>
    /// <returns></returns>
    public static string MapPath(string strPath)
    {
        if (HttpContext.Current != null)
        {
            return HttpContext.Current.Server.MapPath(strPath);
        }
        else //非web程序引用
        {
            strPath = strPath.Replace("/", "\\");
            if (strPath.StartsWith("~"))
            {
                strPath = strPath.Remove(0, 1);
            }
            if (strPath.StartsWith("\\"))
            {
                strPath = strPath.TrimStart('\\');
            }
            return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }

/// <summary>
    /// 读出模版
    /// </summary>
    /// <param name="MBPath">模版路径</param>
    /// <returns></returns>
    public static StringBuilder getMB(string MBPath)
    {
        string mbPath = MapPath(MBPath);
        Encoding code = Encoding.GetEncoding("gb2312");
        StreamReader sr = null;
        StringBuilder sb = new StringBuilder("");
        try
        {
            sr = new StreamReader(mbPath, code);
            string str = sr.ReadToEnd();
            sb.Append(str);
            sr.Close();
            return sb;
        }
        catch
        {
            return null;
        }
        finally
        {

        }
    }

原创粉丝点击