none
Operating system detection; code to detect the operating system running? RRS feed

  • Question

  • Hey,

    I need code to fit for an 'if then' statement based on an operating system. So let's say I am running windows XP, the program detects it, and with a simple if then' statement it opens form2, if i was running vista, it would open form1.

    So basically i need something like: If (WindowXP current OS) then form2.open, If (windowsvista currrent OS) then form1.open

    Of course, the above statement won't work because I need code to support the detection of the O.S.

    Note: I just need code for just these two O.S'es, also I don't care if its Windows XP SP1 2, or 3, just as long as it's an XP.



    Thank you!
    Sunday, March 15, 2009 6:46 AM

Answers

  • http://msdn.microsoft.com/en-us/library/system.operatingsystem.version.aspx
    Dim Major As Integer = Environment.OSVersion.Version.Major  
            Dim Minor As Integer = Environment.OSVersion.Version.Minor  
            If Major = 6 And Minor = 0 Then 
                MessageBox.Show("Vista")  
            ElseIf Major = 5 And Minor = 1 Then 
                MessageBox.Show("Windows XP")  
            ElseIf Major = 6 And Minor = 1 Then 
                MessageBox.Show("Windows 7")  
            Else 
                MessageBox.Show("Go on if needed")  
            End If 
    Edit: Thanks Jie, Corrected the code


    Arjun Paudel
    Sunday, March 15, 2009 8:10 AM

All replies

  •  
     
            Dim SB As New System.Text.StringBuilder  
            Dim OS As Microsoft.VisualBasic.Devices.ComputerInfo = My.Computer.Info  
            SB.Append("OSFullName : " & OS.OSFullName & vbCrLf)  
            SB.Append("OSPlatform : " & OS.OSPlatform & vbCrLf)  
            SB.Append("OSVersion : " & OS.OSVersion)  
              
            MessageBox.Show(SB.ToString)  
     

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Sunday, March 15, 2009 8:03 AM
  • http://msdn.microsoft.com/en-us/library/system.operatingsystem.version.aspx
    Dim Major As Integer = Environment.OSVersion.Version.Major  
            Dim Minor As Integer = Environment.OSVersion.Version.Minor  
            If Major = 6 And Minor = 0 Then 
                MessageBox.Show("Vista")  
            ElseIf Major = 5 And Minor = 1 Then 
                MessageBox.Show("Windows XP")  
            ElseIf Major = 6 And Minor = 1 Then 
                MessageBox.Show("Windows 7")  
            Else 
                MessageBox.Show("Go on if needed")  
            End If 
    Edit: Thanks Jie, Corrected the code


    Arjun Paudel
    Sunday, March 15, 2009 8:10 AM
  • Arjun Paudel said:

    http://msdn.microsoft.com/en-us/library/system.operatingsystem.version.aspx


    If Environment.OSVersion.Version.Major = 6 Then
                MessageBox.Show("Vista")
            ElseIf Environment.OSVersion.Version.Major = 7 Then
                MessageBox.Show("Windows 7")
            ElseIf Environment.OSVersion.Version.Major = 5 Then
                MessageBox.Show("Windows XP")

            End If

    Arjun Paudel



    One major mistake in the code above:

    The version number of Windows 7 is 6.1, not 7. And for Windows XP, the version number is 5.1 and the Server 2003 should be 5.2. Just looking on the Major version can't tell the exact OS. (http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx)

    So we'll have to do this (pseudo code):

    If Major = 5 AndAlso Minor = 0 Then 2000
    If Major = 5 AndAlso Minor = 1 Then XP
    If Major = 5 AndAlso Minor = 2 Then 2003
    If Major = 6 AndAlso Minor = 0 Then Vista
    If Major = 6 AndAlso Minor = 1 Then Windows7

    To make a robust application, we shall be aware of this. And in real code, I suggest do not use literal magic numbers, instead, define and use constants.

    Thanks,
    Jie
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, March 16, 2009 11:16 AM
    Moderator
  • Got you, bad assumption, but forgot to write that, from wikipedia
    Corrected th above code as suggested

    Windows (MS-DOS Based)

    [edit]Windows NT


    Arjun Paudel
    Monday, March 16, 2009 11:44 AM
  • I have an update:

    If Major = 5 AndAlso Minor = 0 Then 2000
    If Major = 5 AndAlso Minor = 1 Then XP
    If Major = 5 AndAlso Minor = 2 Then 2003
    If Major = 6 AndAlso Minor = 0 Then Vista
    If Major = 6 AndAlso Minor = 1 Then Windows7

    If Major = 6 and also Minor =2 then Win 8

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, January 28, 2013 5:31 PM
  •     Private Function OSVersion() As Int32

    'Return a non-zero integer representing an OS. A return of 0 is an error

           Dim version As Integer = 0
            Dim OSV As OperatingSystem = Environment.OSVersion
            If OSV.Version.Major = 5 Then
                Select Case OSV.Version.Minor
                    Case 0 ' Win 2000
                        Return 1
                    Case 1
                        Return 2 ' Xp
                    Case 3
                        Return 3 ' 2003
                End Select
            End If
            If OSV.Version.Major = 6 Then
                Select Case OSV.Version.Minor
                    Case 0
                        Return 4 ' Vista
                    Case 1
                        Return 5 ' Win 7
                    Case 2
                        Return 6 ' Win 8
                End Select
            End If
            Return 0
        End Function


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, January 28, 2013 11:04 PM