• ASP.NET 根据 UserAgent 获取浏览器的类型和版本


    //using System.Text.RegularExpressions;
    public string GetBrowserName(string userAgent, out string browserName, out string ver)
    {
        string fullBrowserName = string.Empty;
        browserName = string.Empty;
        ver = string.Empty;
        // IE
        string regexStr = @"msie (?<ver>[\d.]+)";
        Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
        Match m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Firefox
        regexStr = @"firefox\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Chrome
        regexStr = @"chrome\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Opera
        regexStr = @"opera.([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Safari
        regexStr = @"version\/([\d.]+).*safari";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        return fullBrowserName;
    }
    
  • 相关阅读:
    Swift 对AFN框架的封装
    iOS开发中静态库制作 之.a静态库制作及使用篇
    iOS 地图定位及大头针的基本使用
    swt中改变树的字体及颜色的实现
    为什么很多程序员选择跳槽?
    用SWT做圆形控件
    JAVA简单编码规则
    swt中改变表格字体大小及颜色的实现
    使用JAVA的反射机制反射带有数组参数的私有方法
    我的GIT使用经历
  • 原文地址:https://www.cnblogs.com/anjou/p/3114084.html
Copyright © 2020-2023  润新知