1 Attachment(s)
Determine OS Version and Details (includes Vista support)
I included detection for 95/98/ME even though its highly unlikely that those OS' will be running the Framework and also that 95 doesnt support it. :D
Just wanted to say Thanks to those that helped with getting the final touches working:
VBDT
Negative0
bmahler
There is still a little more work to be done for 2003 but I figure I will post what I have done for now and update it later just so others can start
using it and report any issues that may arise.
I had to trim out the trusts, dll imports and enums to fit but its all included in the zip attachment. ;)
Code attachment written on 2003 SP1
Code:
Option Explicit On
Option Strict On
'VB.NET FAQ Written by RobDog888 (vbforums.com)
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
"Windows Form Designer generated code"
"STRUCTS AND ENUMS"
"DLLS AND CONSTS"
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnDetectOS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetectOS.Click
txtDetectOS.Text = GetOSPlatVerType()
End Sub
Private Function GetOSPlatVerType() As String
'Windows Server "Longhorn" 6.0
'Windows Vista 6.0
'Windows Server 2003 R2 5.2
'Windows Server 2003 5.2
'Windows XP Professional x64 Edition 5.2
'Windows XP Pro 5.1
'Windows XP Home 5.1
'Windows Server 2000 5.0
'Windows 2000 Pro 5.0
'Windows ME 4.90
'Windows 98 4.10
'Windows 95 4.0
'Windows NT4 4.0
Dim osvi As OSVERSIONINFO = New OSVERSIONINFO
Dim xosvi As OSVERSIONINFOEX = New OSVERSIONINFOEX
Dim iRet As Int32 = 0
Dim strDetails As String = String.Empty
osvi.dwOSVersionInfoSize = Marshal.SizeOf(GetType(OSVERSIONINFO))
xosvi.dwOSVersionInfoSize = Marshal.SizeOf(GetType(OSVERSIONINFOEX))
Try
iRet = System.Environment.OSVersion.Platform
If iRet = 1 Then
iRet = GetVersionAdv(osvi)
strDetails = Environment.NewLine & "Version: " & _
osvi.dwMajorVersion & "." & osvi.dwMinorVersion & "." & osvi.dwBuildNumber & _
Environment.NewLine & osvi.szCSDVersion
Select Case Len(osvi)
Case 0
Return "Windows 95" & strDetails
Case 10
Return "Windows 98" & strDetails
Case 9
Return "Windows ME" & strDetails
End Select
Else '2 (NT)
iRet = GetVersionEx(xosvi)
strDetails = Environment.NewLine & "Version: " & _
xosvi.dwMajorVersion & "." & xosvi.dwMinorVersion & "." & xosvi.dwBuildNumber & Environment.NewLine & _
xosvi.szCSDVersion & " (" & xosvi.wServicePackMajor & "." & xosvi.wServicePackMinor & ")"
Select Case xosvi.dwMajorVersion
Case OSMajorVersion.VER_OS_NT4
Return "Windows NT 4" & strDetails
Case OSMajorVersion.VER_OS_2K_XP_2K3
Select Case xosvi.dwMinorVersion
Case 0
'2000
Select Case xosvi.wProductType
Case WinPlatform.VER_NT_WORKSTATION
Return "Windows 2000 Pro" & strDetails
Case WinPlatform.VER_NT_SERVER
If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_DATACENTER) = WinSuiteMask.VER_SUITE_DATACENTER Then
Return "Windows 2000 Datacenter Server" & strDetails
ElseIf (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE) = WinSuiteMask.VER_SUITE_ENTERPRISE Then
Return "Windows 2000 Advanced Server" & strDetails
ElseIf (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_SMALLBUSINESS) = WinSuiteMask.VER_SUITE_SMALLBUSINESS Then
Return "Windows 2000 Small Business Server" & strDetails
Else
Return "Windows 2000 Server" & strDetails
End If
Case WinPlatform.VER_NT_DOMAIN_CONTROLLER
If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_DATACENTER) = WinSuiteMask.VER_SUITE_DATACENTER Then
Return "Windows 2000 Datacenter Server Domain Controller" & strDetails
ElseIf (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE) = WinSuiteMask.VER_SUITE_ENTERPRISE Then
Return "Windows 2000 Advanced Server Domain Controller" & strDetails
ElseIf (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_SMALLBUSINESS) = WinSuiteMask.VER_SUITE_SMALLBUSINESS Then
Return "Windows 2000 Small Business Server Domain Controller" & strDetails
Else
Return "Windows 2000 Server Domain Controller" & strDetails
End If
End Select
Case 1
'XP
If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL) = WinSuiteMask.VER_SUITE_PERSONAL Then
Return "Windows XP Home Edition" & strDetails
Else
Return "Windows XP Professional Edition" & strDetails
End If
Case 2
'2003/Vista
Select Case xosvi.wProductType
Case WinPlatform.VER_NT_WORKSTATION
Return "Windows XP Professional x64 Edition" & strDetails
Case WinPlatform.VER_NT_SERVER
If GetSystemMetrics(SM_SERVERR2) = 1 Then
Return "Windows Server 2003 R2" & strDetails
Else
Return "Windows Server 2003" & strDetails
End If
Case WinPlatform.VER_NT_DOMAIN_CONTROLLER
If GetSystemMetrics(SM_SERVERR2) = 1 Then
Return "Windows Server 2003 R2 Domain Controller" & strDetails
Else
Return "Windows Server 2003 Domain Controller" & strDetails
End If
End Select
End Select
Case OSMajorVersion.VER_OS_VISTA_LONGHORN
If xosvi.wProductType = WinPlatform.VER_NT_WORKSTATION Then
If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL) = WinSuiteMask.VER_SUITE_PERSONAL Then
Return "Windows Vista (Home Premium, Home Basic, or Home Ultimate) Edition"
Else
Return "Windows Vista (Enterprize or Business)" & strDetails
End If
Else
Return "Windows Server (Longhorn)" & strDetails
End If
End Select
End If
Catch
MessageBox.Show(GetLastError.ToString)
Return String.Empty
End Try
End Function
End Class
Re: Determine OS Version and Details (includes Vista support)
Re: Determine OS Version and Details (includes Vista support)
good work, now if only i'd looked on here at the start of te day i would have saved myself alot of searching and piecing together bits of other peoples code to find out all you have put :)
Re: Determine OS Version and Details (includes Vista support)
We all have days like that. :)
Did you find anything additional that could be added to the code? Does it work correctly for you?
Re: Determine OS Version and Details (includes Vista support)
possibly add the service pack level as well?? Ive not actually tried your code but it looks similar to mine.
Re: Determine OS Version and Details (includes Vista support)
Service pack info is in the strDetails variable along with the build version.
Re: Determine OS Version and Details (includes Vista support)
Been looking for this for vb6
Does anyone have code like this for vb6
Re: Determine OS Version and Details (includes Vista support)
Its practically the same. Not too hard to convert it to VB 6.
Re: Determine OS Version and Details (includes Vista support)
Re: Determine OS Version and Details (includes Vista support)
Here is a pretty basic program, all it requires is a form with four buttons, and it will work properly, this is the entire code, hope you like :D. By the way, the information is shown in a Messagebox.
VB Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'
'Returns Operating System Name, Platform and Version
'
MsgBox(" Operating System Name: " & My.Computer.Info.OSFullName & vbNewLine & " Operating System Platform: " & My.Computer.Info.OSPlatform.ToString & vbNewLine & " Operating System Version: " & My.Computer.Info.OSVersion.ToString, , )
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'
'Returns the total amount of Physical and Virtual Memory for the Computer.
'
MsgBox(" Total Physical Memory: " & My.Computer.Info.TotalPhysicalMemory.ToString & " bytes" & vbNewLine & " Total Virtual Memory: " & My.Computer.Info.TotalVirtualMemory.ToString & " bytes ")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'
'Get the displays current Resolution.
'
MsgBox(" Current Resolution: " & My.Computer.Screen.Bounds.Width.ToString & "x" & My.Computer.Screen.Bounds.Height.ToString & " Pixels")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'
'Get the amount of Physical and Virtual Memory that is available.
'
MsgBox(" Available Physical Memory: " & My.Computer.Info.AvailablePhysicalMemory.ToString & " bytes" & vbNewLine & " Available Virtual Memory: " & My.Computer.Info.AvailableVirtualMemory.ToString & " bytes")
End Sub
End Class
Re: Determine OS Version and Details (includes Vista support)
Yes, thats the easy way when you only need simple OS name or version. My code gets the suitemasks for a more detailed return. ;)
Re: Determine OS Version and Details (includes Vista support)
To expand on this since MS has announced that Windows 7's internal version # is 6.2, here's the function that includes that:
Code:
Friend Function GetOSVersion() As String
Dim strVersion As String = "Unknown"
Select Case Environment.OSVersion.Platform
Case PlatformID.Win32S
strVersion = "Windows 3.1"
Case PlatformID.Win32Windows
Select Case Environment.OSVersion.Version.Minor
Case 0I
strVersion = "Windows 95"
Case 10I
If Environment.OSVersion.Version.Revision.ToString() = "2222A" Then
strVersion = "Windows 98 Second Edition"
Else
strVersion = "Windows 98"
End If
Case 90I
strVersion = "Windows ME"
End Select
Case PlatformID.Win32NT
Select Case Environment.OSVersion.Version.Major
Case 3I
strVersion = "Windows NT 3.51"
Case 4I
strVersion = "Windows NT 4.0"
Case 5I
Select Case Environment.OSVersion.Version.Minor
Case 0I
strVersion = "Windows 2000"
Case 1I
strVersion = "Windows XP"
Case 2I
strVersion = "Windows 2003"
End Select
Case 6I
Select Case Environment.OSVersion.Version.Minor
Case 0I
strVersion = "Windows Vista"
Case 1I
strVersion = "Windows 2008"
Case 2I
strVersion = "Windows 7"
End Select
End Select
Case PlatformID.WinCE
strVersion = "Windows CE"
Case PlatformID.Unix
strVersion = "Unix"
End Select
Return strVersion
End Function
Re: Determine OS Version and Details (includes Vista support)
If you simply want the name and version, than the WMI class WIN32_Operating_System contains the full description as well :)
(Including versions for Vista and Server)
Re: Determine OS Version and Details (includes Vista support)
Well my original code determines what flavor of Vista as well as other OS' which wont show in in the WMI classes. Just depends upon how much detail you need/want
Re: Determine OS Version and Details (includes Vista support)
EDIT:
Quote:
Windows 7's internal version # is 6.2
Weird, cause my Windows 7 Ultimate's Internal number is 6.1
vb.net Code:
Case OSMajorVersion.VER_OS_VISTA_LONGHORN
Select Case xosvi.dwMinorVersion
Case 0
If xosvi.wProductType = WinPlatform.VER_NT_WORKSTATION Then
If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL) = WinSuiteMask.VER_SUITE_PERSONAL Then
Return "Windows Vista (Home Premium, Home Basic, or Home Ultimate) Edition"
Else
Return "Windows Vista (Enterprize or Business)" & strDetails
End If
Else
Return "Windows Server (Longhorn)" & strDetails
End If
Case 1
Return "Windows 7"
End Select
This addon will do basic Windows 7 checking...and I mean basic :)
Re: Determine OS Version and Details (includes Vista support)
Code:
If Environment.OSVersion.Platform = PlatformID.Win32NT AndAlso Environment.OSVersion.Version.Major = 6I AndAlso Environment.OSVersion.Version.Minor = 1I Then
'Running Win7 (any)
End If
Re: Determine OS Version and Details (includes Vista support)
Quote:
Originally Posted by
JuggaloBrotha
Code:
If Environment.OSVersion.Platform = PlatformID.Win32NT AndAlso Environment.OSVersion.Version.Major = 6I AndAlso Environment.OSVersion.Version.Minor = 1I Then
'Running Win7 (any)
End If
That IF statement will not only return true for Windows 7 - it will return true for Server 2008 R2 as well as that has the same version number ;)