如何使用内部版本号在VB.Net中标识OS

用户名

我有一个使用System.Environment.OSVersion.Version.Major的现有应用程序,以标识该应用程序在其上运行的操作系统。我使用它来确保应用程序不在不受支持的操作系统上运行。

我目前遇到一个障碍,因为我需要能够确定用户是否正在使用不带SP的Vista,Vista SP1,Vista SP2,Windows 7,Windows 7 SP1,Server 2008,Server 2008 R2,Windows 8和Windows 8 SP1作为应用程序中的某些设置取决于Service Pack版本。

根据我的查找,Windows 7和Server 2008共享相同的内部版本号,因此这也是一个问题,微软对未成年人进行版本编号的方式也会导致我当前的代码无法正常工作。

内部编号

Vista                       6.0.6000 
Vista SP1 ????????          6.0.6001.18000
Vista SP2                   6.0.6002
Windows Server 2008         6.0.6001
Windows 7                   6.1.7600
Windows 7 SP1               6.1.7601
Windows Server 2008 R2      6.1.7600
Windows Server 2008 R2 SP1 ???  6.0.7601

他们是执行此操作的更好方法还是System.Environment.Version的扩展功能以使用内部版本号?我花了几个小时寻找无济于事的答案。

我当前的代码如下

  Select Case System.Environment.OSVersion.Version.Major
        Case 5
            Select Case _
                System.Environment.OSVersion.Version.Minor
                Case 0
                    Dim frm As New Win2000
                    frm.Show(Win2000)
                Case 1
                    Dim frm As New XP
                    frm.Show(XP)
                Case 2
                    Dim frm As New Win2003
                    frm.Show(Win2003)
                Case Else
                    Dim frm As New Unsupported
                    frm.Show(Unsupported)
            End Select
        Case 6
            Select Case _
                System.Environment.OSVersion.Version.Minor
                Case 0
                    If My.Computer.Info.OSFullName.Contains("Home Basic") Then
                        Dim frm As New VistaHB
                        frm.Show(VistaHB)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Home Premium") Then

                        Dim frm As New VistaPre
                        frm.Show(VistaPre)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Ultimate") Then

                        Dim frm As New VistaUlt
                        frm.Show(VistaUlt)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Business") Then
                        Dim frm As New VistaB
                        frm.Show(VistaB)
                    End If
                Case 1
                    If My.Computer.Info.OSFullName.Contains("Home Basic") Then
                        Dim frm As New Win7HB
                        frm.Show(Win7HB)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Home Premium") Then

                        Dim frm As New Win7Pre
                        frm.Show(Win7Pre)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Ultimate") Then

                        Dim frm As New Win7Ult
                        frm.Show(Win7Ult)
                    End If
                    If My.Computer.Info.OSFullName.Contains("Business") Then
                        Dim frm As New Win7biz
                        frm.Show(Win7biz)
                    End If

                Case Else
                    Dim frm As New Unsupported
                    frm.Show(Unsupported)
            End Select
    End Select
ʀ

我使用下面的链接来帮助我尝试一次找出相同的事物。

http://www.vb-helper.com/howto_net_get_os_name.html

取自上面的链接:

WMI(Windows管理规范)使您可以使用类似SQL的语句来向计算机询问有关其自身的信息。本示例使用它来获取:

•操作系统名称,包括其版本(Home,Ultimate等),版本和Service Pack编号•逻辑处理器的数量•系统使用的位数(32或64)

程序加载时,以下代码在标签中显示这些值。

 ' Display the operating system's name. Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Get the OS information.
' For more information from this query, see:
'      http:'msdn.microsoft.com/library/aa394239.aspx
Dim os_query As String = "SELECT * FROM " & _
    "Win32_OperatingSystem"
Dim os_searcher As New _
    ManagementObjectSearcher(os_query)
For Each info As ManagementObject In os_searcher.Get()
    lblCaption.Text = _
        info.Properties("Caption").Value.ToString().Trim()
    lblVersion.Text = "Version " & _
        info.Properties("Version").Value.ToString() & _
        " SP " & _
        info.Properties("ServicePackMajorVersion").Value.ToString() _
            & "." & _
    info.Properties("ServicePackMinorVersion").Value.ToString()
Next info

' Get number of processors.
' For more information from this query, see:
'      http:'msdn.microsoft.com/library/aa394373.aspx
Dim cpus_query As String = "SELECT * FROM " & _
    "Win32_ComputerSystem"
Dim cpus_searcher As New _
    ManagementObjectSearcher(cpus_query)
For Each info As ManagementObject In cpus_searcher.Get()
    lblCpus.Text = _
        info.Properties("NumberOfLogicalProcessors").Value.ToString() _
            & _
        " processors"
Next info

' Get 32- versus 64-bit.
' For more information from this query, see:
'      http:'msdn.microsoft.com/library/aa394373.aspx
Dim proc_query As String = "SELECT * FROM " & _
    "Win32_Processor"
Dim proc_searcher As New _
    ManagementObjectSearcher(proc_query)
For Each info As ManagementObject In proc_searcher.Get()
    lblBits.Text = _
        info.Properties("AddressWidth").Value.ToString() _
        & "-bit"
Next info End Sub

 The code uses the WMI query "SELECT * FROM Win32_OperatingSystem" to get information about the operating system. The result is a

集合包含一个Win32_OperatingSystem对象。该代码使用循环来获取该对象并读取其属性。Caption属性提供完整的操作系统名称,包括商标符号和版本。版本返回操作系统的版本。ServicePackMajorVersion和ServicePackMinorVersion属性提供服务包信息。

接下来,程序以类似的方式执行查询“ SELECT * FROM Win32_ComputerSystem”,以获取逻辑处理器的数量。

通过执行查询“ SELECT * FROM Win32_Processor”来确定这是32位还是64位操作系统来完成。

请注意,使用WMI要求您添加对System.Management的引用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章