WebHelper类

成员一:获取上下文对象:

        public static HttpContext GetContext()
        {
            HttpContext context = HttpContext.Current;
            if (context == null)
            {
                throw new Exception("HttpContext not found");  
            }
            return context;
        }

成员二:获取当前程序集的版本号:

       public static string GetVersion()
        {
            if (string.IsNullOrEmpty(WebHelper._version))
            {
                int majorVersion = typeof(WebHelper).Assembly.GetName().Version.Major;
                WebHelper._version = majorVersion.ToString();
            }
            return WebHelper._version;
        }

成员三:获取当前程序集中的资源字节数组

        public static byte[] LoadAssemblyFiles(string fullResourceName)
        {
            if (string.IsNullOrEmpty(fullResourceName))
                throw new ArgumentNullException("fullResourceName");
            byte[] fileContent;
            using (Stream stream = typeof(WebHelper).Assembly.GetManifestResourceStream(fullResourceName))
            {
                fileContent = new byte[stream.Length];
                stream.Read(fileContent, 0, fileContent.Length);
            }
            return fileContent;
        }

注意:红色字体的WebHelper是这个函数成员所在的类,具体应用时,应该替换为实际的类名。例如如果这个成员是位于WebUtility类中,你必须将红色字体处改成WebUtility。

成员四:从Cache中读取缓存的资源字节数组。

        private static byte[] LoadFileFromCache(string fullResourceName)
        {
            byte[] buffer;
            HttpContext context = GetContext();
            if (context.Cache[fullResourceName] == null)
            {
                context.Cache[fullResourceName] = LoadAssemblyFiles(fileName);
            }
            buffer = (byte[])context.Cache[fullResourceName];
            return buffer;
        }

成员五:从程序集中读取资源文件(字符串)

        public static StringBuilder GetHtml(string fullResourceName)
        {
            byte[] buffer = LoadFileFromCache(fullResourceName);
            if (buffer == null || buffer.Length == 0)
            {
                throw new ArgumentNullException("fullResourceName", ("isn't find " + fullResourceName));
            }
            return new StringBuilder(Encoding.Default.GetString(buffer));
        }

成员六:判断IE浏览器的版本。

        public static bool isAccordantBrowser()
        {
            HttpBrowserCapabilities bc = GetContext().Request.Browser;
            if (bc.Browser != "IE" || float.Parse(bc.Version) < 5.5)
            {
                return false;
            }
            return true;
        }

成员七:转换文件长度为字符串

        public static string GetFormatString(double size)
        {
            string sizeString = string.Empty;
            if (size >= 1048576)
            {
                sizeString = (Math.Round(size / 1048576, 2) + "MB");
            }
            else if (size > 1024)
            {
                sizeString = (Math.Round(size / 1024, 2) + "KB");
            }
            else
            {
                sizeString = (size + "B");
            }
            return sizeString;
        }

成员八:转换时间变量字符串

        public static string GetFormatString(TimeSpan span)
        {
            string timeString = string.Empty;
            if (span.Days > 0 || span.Hours > 0)
            {
                int hours = ((0x18 * span.Days) + span.Hours);
                timeString = (timeString + hours + "&nbsp;Hour(s)&nbsp;");
            }
            if (span.Minutes > 0)
            {
                timeString = (timeString + span.Minutes + "&nbsp;Minute(s)&nbsp;");
            }
            if (span.Seconds > 0)
            {
                timeString = (timeString + span.Seconds + "&nbsp;Second(s)&nbsp;");
            }
            return timeString;
        }

转载于:https://www.cnblogs.com/jameszou/articles/1322327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值