取得系統版本

使用API

API名稱:GetVersion
動態連結程式庫: kernel32.dll


程式說明

取得系統目前版本
此為廢棄API,Windows NT以後版本號為6.02( VB6為5.01 )
需要查詢正確版本號請使用WMI。
另外可以判斷NT或95、98,但是擺在這個世代,已經沒有意義。


VB6程式碼

Private Declare Function GetVersion Lib "kernel32" () As Long

Private Sub Command1_Click()
    lngVer = GetVersion()
    lngWinVer = lngVer And &HFFFF&
    ' 取得 Windows 版本
    GetWinVer = Format((lngWinVer Mod 256) + ((lngWinVer \ 256) / 100), "Fixed")
    Text1.Text = GetWinVer
End Sub

Private Sub Command2_Click()
    If GetVersion() And &H80000000 Then
        Text2.Text = "Running under Windows 95/98"
    Else
        Text2.Text = "Running under Windows NT"
    End If
End Sub

Private Sub Form_Load()
    Text3.Text = GetVersion()
End Sub

VB.NET程式碼

Public Class Form1
    Private Declare Function GetVersion Lib "kernel32" () As Integer
    Public Function GetWinVer() As String
        Dim lngVer As Long, lngWinVer As Long
        lngVer = GetVersion()
        lngWinVer = lngVer And &HFFFF&
        ' 取得 Windows 版本
        GetWinVer = Format((lngWinVer Mod 256) + ((lngWinVer \ 256) / 100), "Fixed")
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = GetWinVer()
        TextBox2.Text = GetVersion()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If GetVersion() And &H80000000 Then
            MsgBox("Running under Windows 95/98")
        Else
            MsgBox("Running under Windows NT")
        End If
    End Sub
End Class

下載