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;
        }

posted on 2008-10-29 17:51  邹华栋  阅读(3441)  评论(0编辑  收藏  举报