日期:2014-05-17  浏览次数:20408 次

亮亮大家常用的公共类吧!

刚到一新衙门,发现老的项目中居然啊公共类都没有的。
什么,替换文本中HTML标签内容,和截取字符串长度(按照字节截取),这些都没的。

大家亮亮自己的吧!


------解决方案--------------------
http://topic.csdn.net/u/20090815/08/1AA82791-A7A2-4640-80B4-907F5771D676.html
------解决方案--------------------
http://blog.csdn.net/anchenyanyue
------解决方案--------------------
贴几个我项目中用到的。
C# code

/// <summary>
    /// 截取指定长度中英文字符串(宽度一样)
    /// </summary>
    /// <param name="str">要截取的字符串</param>
    /// <param name="length">截取长度,中文字符长度</param>
    /// <returns>截取后的字符串</returns>
    public static string CutStr(object str, int length)
    {
        if (str == null) return string.Empty;

        Encoding encoding = Encoding.GetEncoding("gb2312");

        int len = length * 2;
        int j = 0, k = 0;

        string cutStr = str.ToString();

        for (int i = 0; i < cutStr.Length; i++)
        {
            byte[] bytes = encoding.GetBytes(cutStr.Substring(i, 1));
            if (bytes.Length == 2)//不是英文
                j += 2;
            else
                j++;

            if (j <= len)
                k += 1;

            if (j >= len)
                return cutStr.Substring(0, k) + "...";
        }
        return cutStr;
    }

    /// <summary>
    /// 下载指定路径文件
    /// </summary>
    /// <param name="path">文件绝对路径</param>
    public static void DownLoadFile(string path)
    {
        System.IO.FileInfo fi = new System.IO.FileInfo(path);
        if (fi.Exists)
        {
            //判断文件是否正在使用
            try
            {
                using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
                {
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(path.Substring(path.LastIndexOf("\\") + 1), System.Text.Encoding.UTF8));
            System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream;charset=gb2321";
            System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.Close();
        }
        else
        {
            System.Web.HttpContext.Current.Response.Write("<script>alert('源文件不存在!');</script>");
        }
    }
    /// <summary>
    /// 获取枚举的描述信息
    /// </summary>
    /// <param name="en">枚举</param>
    /// <returns></returns>
    public static string GetEnumDescription(this Enum en)
    {
        Type type = en.GetType();
        System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
            if (attrs != null && attrs.Length > 0)
                return ((System.ComponentModel.DescriptionAttribute)attrs[0]).Description;
        }
        return en.ToString();
    }

/// <summary>
/// 通用应用程序缓存辅助类
/// </summary>
public static class CacheHelper
{
    public delegate T GetDataMethod<T>();//获取数据的方法

    /// <summary>
    /// 通用应用程序缓存方法,缓存数据10分钟
    /// </summary>
    /// <typeparam name="T">缓存数据的类型,一般是集合,如IList&lt;User