zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Danny_Su的devexpress 9.3.3的注册插件在vs2010RC中无法使用的解决办法
    EntitySpaces2009中的关系
    EntitySpaces2009的开发文档地址
    EntitySpaces2009支持事务
    EntitySpaces2009中连接Access的连接设置
    基于matlab的视频测速处理
    图像拼接与融合
    某图像分析系统
    黑子数与开盘指数相关性分析
    另类的图像处理
  • 原文地址:https://www.cnblogs.com/anjou/p/3114084.html
Copyright © 2011-2022 走看看