how do I detect user operating system

前端 未结 11 829
难免孤独
难免孤独 2020-12-01 03:58

I have the following code to obtain user details:

HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
string UserAgent = HttpContext.Current.Re         


        
相关标签:
11条回答
  • 2020-12-01 04:11
    private string getOS()
    {
        string os = null;
        if (Request.UserAgent.IndexOf("Windows NT 5.1") > 0)
        {
            os ="Windows XP";
            return os;
        }
        else if (Request.UserAgent.IndexOf("Windows NT 6.0") > 0)
        {
            os= "Windows Vista";
            return os;
        }
        else if (Request.UserAgent.IndexOf("Windows NT 6.1") > 0)
        {
            os = "Windows 7";
            return os;
        }
        else if (Request.UserAgent.IndexOf("Intel Mac OS X") > 0)
        {
            //os = "Mac OS or older version of Windows";
            os = "Intel Mac OS X";
            return os;
        }
        else
        {
            os = "You are using older version of Windows or Mac OS";
            return os;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 04:12

    There is a cool tool named: https://github.com/ua-parser/uap-csharp
    that parse the user agent to OS,Browser,Browser version etc...
    Link to Nuget

    And this is how used it:

     public static string GetUserOS(string userAgent)
     {
        // get a parser with the embedded regex patterns
        var uaParser = Parser.GetDefault();
        ClientInfo c = uaParser.Parse(userAgent);
        return c.OS.Family;
     }
    
    0 讨论(0)
  • 2020-12-01 04:14

    Use Request.UserAgent

    if (Request.UserAgent.IndexOf("Windows NT 5.1") > 0)
    {
    //xp
    }
    else if (Request.UserAgent.IndexOf("Windows NT 6.0") > 0)
    {
    //VISTA
    }
    else if (Request.UserAgent.IndexOf("Windows NT 6.1") > 0)
    {
    //7
    }
    else if (Request.UserAgent.IndexOf("Windows NT 6.2") > 0) 
    { 
    //8
    }
    else if (Request.UserAgent.IndexOf("Windows NT 6.3") > 0) 
    { 
    //8.1
    }
    else if (Request.UserAgent.IndexOf("Windows NT 10.0") > 0) 
    { 
    //10
    }
    
    0 讨论(0)
  • 2020-12-01 04:14

    According to This Official Microsoft Document we can use this to detect Windows OS:

    String userAgent = Request.UserAgent;
    
    if (userAgent.IndexOf("Windows NT 6.3") > 0)
    {
        //Windows 8.1
    }
    else if (userAgent.IndexOf("Windows NT 6.2") > 0)
    {
        //Windows 8
    }
    else if (userAgent.IndexOf("Windows NT 6.1") > 0)
    {
        //Windows 7
    }
    else if (userAgent.IndexOf("Windows NT 6.0") > 0) 
    { 
        //Windows Vista
    }
    else if (userAgent.IndexOf("Windows NT 5.2") > 0) 
    { 
        //Windows Server 2003; Windows XP x64 Edition
    }
    else if (userAgent.IndexOf("Windows NT 5.1") > 0) 
    { 
        //Windows XP
    }
    else if (userAgent.IndexOf("Windows NT 5.01") > 0) 
    { 
        //Windows 2000, Service Pack 1 (SP1)
    }
    else if (userAgent.IndexOf("Windows NT 5.0") > 0) 
    { 
        //Windows 2000
    }
    else if (userAgent.IndexOf("Windows NT 4.0") > 0) 
    { 
        //Microsoft Windows NT 4.0
    }
    else if (userAgent.IndexOf("Win 9x 4.90") > 0) 
    { 
        //Windows Millennium Edition (Windows Me)
    }
    else if (userAgent.IndexOf("Windows 98") > 0) 
    { 
        //Windows 98
    }
    else if (userAgent.IndexOf("Windows 95") > 0) 
    { 
        //Windows 95
    }
    else if (userAgent.IndexOf("Windows CE") > 0) 
    { 
        //Windows CE
    }
    else
    { 
        //Others
    }
    
    0 讨论(0)
  • 2020-12-01 04:17

    Here's what I came up with and it seems to work fairly well:

    public String GetUserEnvironment(HttpRequest request)
    {
        var browser = request.Browser;
        var platform = GetUserPlatform(request);
        return string.Format("{0} {1} / {2}", browser.Browser, browser.Version, platform);
    }
    
    public String GetUserPlatform(HttpRequest request)
    {
        var ua = request.UserAgent;
    
        if (ua.Contains("Android"))
            return string.Format("Android {0}", GetMobileVersion(ua, "Android"));
    
        if (ua.Contains("iPad"))
            return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS"));
    
        if (ua.Contains("iPhone"))
            return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS"));
    
        if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
            return "Kindle Fire";
    
        if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
            return "Black Berry";
    
        if (ua.Contains("Windows Phone"))
            return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone"));
    
        if (ua.Contains("Mac OS"))
            return "Mac OS";
    
        if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
            return "Windows XP";
    
        if (ua.Contains("Windows NT 6.0"))
            return "Windows Vista";
    
        if (ua.Contains("Windows NT 6.1"))
            return "Windows 7";
    
        if (ua.Contains("Windows NT 6.2"))
            return "Windows 8";
    
        if (ua.Contains("Windows NT 6.3"))
            return "Windows 8.1";
    
        if (ua.Contains("Windows NT 10"))
            return "Windows 10";
    
        //fallback to basic platform:
        return request.Browser.Platform + (ua.Contains("Mobile") ? " Mobile " : "");
    }
    
    public String GetMobileVersion(string userAgent, string device)
    {
        var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart();
        var version = string.Empty;
    
        foreach (var character in temp)
        {
            var validCharacter = false;
            int test = 0;
    
            if (Int32.TryParse(character.ToString(), out test))
            {
                version += character;
                validCharacter = true;
            }
    
            if (character == '.' || character == '_')
            {
                version += '.';
                validCharacter = true;
            }
    
            if (validCharacter == false)
                break;
        }
    
        return version;
    }
    
    0 讨论(0)
  • 2020-12-01 04:17

    Try this I just Modify

    string device = getOSInfo();
    
    public String getOSInfo()
    {
        var ua = Request.UserAgent;
    
        if (ua.Contains("Android"))
            return string.Format("Android {0}", GetMobileVersion(ua, "Android"));
    
        if (ua.Contains("iPad"))
            return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS"));
    
        if (ua.Contains("iPhone"))
            return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS"));
    
        if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
            return "Kindle Fire";
    
        if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
            return "Black Berry";
    
        if (ua.Contains("Windows Phone"))
            return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone"));
    
        if (ua.Contains("Mac OS"))
            return "Mac OS";
    
        if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
            return "Windows XP";
    
        if (ua.Contains("Windows NT 6.0"))
            return "Windows Vista";
    
        if (ua.Contains("Windows NT 6.1"))
            return "Windows 7";
    
        if (ua.Contains("Windows NT 6.2"))
            return "Windows 8";
    
        if (ua.Contains("Windows NT 6.3"))
            return "Windows 8.1";
    
        if (ua.Contains("Windows NT 10"))
            return "Windows 10";
    
        //fallback to basic platform:
        return (ua.Contains("Mobile") ? " Mobile " : "");
    }
    public String GetMobileVersion(string userAgent, string device)
    {
        var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart();
        var version = string.Empty;
    
        foreach (var character in temp)
        {
            var validCharacter = false;
            int test = 0;
    
            if (Int32.TryParse(character.ToString(), out test))
            {
                version += character;
                validCharacter = true;
            }
    
            if (character == '.' || character == '_')
            {
                version += '.';
                validCharacter = true;
            }
    
            if (validCharacter == false)
                break;
        }
    
        return version;
    }
    
    0 讨论(0)
提交回复
热议问题