how do I detect user operating system

前端 未结 11 830
难免孤独
难免孤独 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:27

    The VB.NET answer: I include it only because it might be easier to read and understand.

    Public Function GetOS() As String
        Dim MyAgent As String = Current.Request.UserAgent
        If MyAgent.IndexOf("Windows NT 10.0") >= 0 Then
            Return "Windows 10"
        ElseIf MyAgent.IndexOf("Windows NT 6.3") >= 0 Then
            Return "Windows 8.1"
        ElseIf MyAgent.IndexOf("Windows NT 6.2") >= 0 Then
            Return "Windows 8"
        ElseIf MyAgent.IndexOf("Windows NT 6.1") >= 0 Then
            Return "Windows 7"
        ElseIf MyAgent.IndexOf("Windows NT 6.0") >= 0 Then
            Return "Windows Vista"
        ElseIf MyAgent.IndexOf("Windows NT 5.2") >= 0 Then
            Return "Windows Server 2003"
        ElseIf MyAgent.IndexOf("Windows NT 5.1") >= 0 Then
            Return "Windows XP"
        ElseIf MyAgent.IndexOf("Windows NT 5.01") >= 0 Then
            Return "Windows 2000 (SP1)"
        ElseIf MyAgent.IndexOf("Windows NT 5.0") >= 0 Then
            Return "Windows 2000"
        ElseIf MyAgent.IndexOf("Windows NT 4.5") >= 0 Then
            Return "Windows NT 4.5"
        ElseIf MyAgent.IndexOf("Windows NT 4.0") >= 0 Then
            Return "Windows NT 4.0"
        ElseIf MyAgent.IndexOf("Win 9x 4.90") >= 0 Then
            Return "Windows ME"
        ElseIf MyAgent.IndexOf("Windows 98") >= 0 Then
            Return "Windows 98"
        ElseIf MyAgent.IndexOf("Windows 95") >= 0 Then
            Return "Windows 95"
        ElseIf MyAgent.IndexOf("Windows CE") >= 0 Then
            Return "Windows CE"
        ElseIf (MyAgent.Contains("iPad")) Then
            Return String.Format("iPad OS {0}", GetMobileVersion(MyAgent, "OS"))
        ElseIf (MyAgent.Contains("iPhone")) Then
            Return String.Format("iPhone OS {0}", GetMobileVersion(MyAgent, "OS"))
        ElseIf (MyAgent.Contains("Linux") AndAlso MyAgent.Contains("KFAPWI")) Then
            Return "Kindle Fire"
        ElseIf (MyAgent.Contains("RIM Tablet") OrElse (MyAgent.Contains("BB") AndAlso MyAgent.Contains("Mobile"))) Then
            Return "Black Berry"
        ElseIf (MyAgent.Contains("Windows Phone")) Then
            Return String.Format("Windows Phone {0}", GetMobileVersion(MyAgent, "Windows Phone"))
        ElseIf (MyAgent.Contains("Mac OS")) Then
            Return "Mac OS"
        ElseIf MyAgent.IndexOf("ANDROID") >= 0 Then
            Return String.Format("Android {0}", GetMobileVersion(MyAgent, "ANDROID"))
        Else
            Return "OS is unknown."
        End If
    End Function
    
    Private Function GetMobileVersion(userAgent As String, device As String) As String
        Dim ReturnValue As String = String.Empty
        Dim RawVersion As String = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart()
        For Each character As Char In RawVersion
            If IsNumeric(character) Then
                ReturnValue &= character
            ElseIf (character = "." OrElse character = "_") Then
                ReturnValue &= "."
            Else
                Exit For
            End If
        Next
        Return ReturnValue
    End Function
    
    0 讨论(0)
  • 2020-12-01 04:27

    John the VB function is nice but the line for Windows 10 does not work because you have "WINDOWS" in uppercase.

    It should be same as others i.e.

    If MyAgent.IndexOf("Windows NT 10.0") >= 0 Then
            Return "Windows 10"
    
    0 讨论(0)
  • 2020-12-01 04:31

    Use the version number after "Windows NT". Windows 7 has 6.1 as version number.

    But don't rely too much on that, user agent string is non standard. For example look this list, you'll see that someone is using Internet Explorer with Windows 9.0!

    0 讨论(0)
  • 2020-12-01 04:33

    There's no accurate way of doing so as all the information you get from user request's headers which can easily be changed by user and can contain just anything.

    If you're OK with reading probably inaccurate information then you may want to check this SO answer to similar question

    0 讨论(0)
  • 2020-12-01 04:36

    All In One Class

      public class OS
            {
                public string os_name { get; set; }
                public string os_version { get; set; }
    
                public OS()
                {
                    var ua = HttpContext.Current.Request.UserAgent;
                    if (ua.Contains("Android"))
                    {
                        this.os_name = "Android";
                        SetVersion(ua, "Android");
                        return;
                    }
    
                    if (ua.Contains("iPhone"))
                    {
                        this.os_name = "iPhone";
                        SetVersion(ua, "OS");
                        return;
                    }
    
                    if (ua.Contains("iPad"))
                    {
                        this.os_name = "iPad";
                        SetVersion(ua, "OS");
                        return;
                    }
    
                    if (ua.Contains("Mac OS"))
                    {
                        this.os_name = "Mac OS";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 10"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "10";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 6.3"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "8.1";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 6.2"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "8";
                        return;
                    }
    
    
                    if (ua.Contains("Windows NT 6.1"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "7";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 6.0"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "Vista";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
    
                    {
                        this.os_name = "Windows";
                        this.os_version = "XP";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 5"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "2000";
                        return;
                    }
    
                    if (ua.Contains("Windows NT 4"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "NT4";
                        return;
                    }
    
                    if (ua.Contains("Win 9x 4.90"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "Me";
                        return;
                    }
    
                    if (ua.Contains("Windows 98"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "98";
                        return;
                    }
    
                    if (ua.Contains("Windows 95"))
                    {
                        this.os_name = "Windows";
                        this.os_version = "95";
                        return;
                    }
    
    
                    if (ua.Contains("Windows Phone"))
                    {
                        this.os_name = "Windows Phone";
                        SetVersion(ua, "Windows Phone");
                        return;
                    }
    
                    if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
                    {
                        this.os_name = "Kindle Fire";
                        return;
                    }
    
                    if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
                    {
                        this.os_name = "Black Berry";
                        return;
                    }
    
                    //fallback to basic platform:
                    this.os_name = request.Browser.Platform + (ua.Contains("Mobile") ? " Mobile " : "");
                }
    
                private void SetVersion(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;
                    }
                    this.os_version = version;
                }
            }
    

    Usage

    var os = new OS();
    os.os_name; // here is os name
    os.os_version; // here is os vers
    
    0 讨论(0)
提交回复
热议问题