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 + " Hour(s) ");
??????????? }
??????????? if (span.Minutes > 0)
??????????? {
??????????????? timeString = (timeString + span.Minutes + " Minute(s) ");
??????????? }
??????????? if (span.Seconds > 0)
??????????? {
??????????????? timeString = (timeString + span.Seconds + " Second(s) ");
??????????? }
??????????? return timeString;
??????? }
轉載于:https://www.cnblogs.com/jameszou/articles/1322327.html
總結
以上是生活随笔為你收集整理的WebHelper类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无影好还是天魔斩好
- 下一篇: 帝国时代4什么配置能玩