在.NET中轻松得到OS的版本号(小议)
没事做就想来写点东西,其实没什么大不了的,相信谁都会
就是谁都会,我为什么要写?费话闲着没事.OK,开始吧,首先
介绍一下一个API函数.GetVersionEX,这个函数用来返回一个
结构OVSVERSIONINFO该结构用来记录版本信息.以下是一段VB的代码 Option Explicit
Private Const VER_PLATFORM_WIN32s = 0 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_NT_WORKSTATION = 1 Private Const VER_NT_DOMAIN_CONTROLLER = 2 Private Const VER_NT_SERVER = 3
Private Type OSVERSIONINFOEX dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 ´ Maintenance string for PSS usage wServicePackMajor As Integer ´win2000 only wServicePackMinor As Integer ´win2000 only wSuiteMask As Integer ´win2000 only wProductType As Byte ´win2000 only wReserved As Byte End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFOEX) As Long
Private Function GetVersionInfo() As String Dim myOS As OSVERSIONINFOEX Dim bExInfo As Boolean Dim sOS As String
myOS.dwOSVersionInfoSize = Len(myOS) ´should be 148/156 ´try win2000 version If GetVersionEx(myOS) = 0 Then ´if fails myOS.dwOSVersionInfoSize = 148 ´ignore reserved data If GetVersionEx(myOS) = 0 Then GetVersionInfo = "Microsoft Windows (Unknown)" Exit Function End If Else bExInfo = True End If With myOS ´is version 4 If .dwPlatformId = VER_PLATFORM_WIN32_NT Then ´nt platform Select Case .dwMajorVersion Case 3, 4 sOS = "Microsoft Windows NT" Case 5 sOS = "Microsoft Windows 2000" End Select If bExInfo Then ´workstation/server? If .wProductType = VER_NT_SERVER Then sOS = sOS & " Server" ElseIf .wProductType = VER_NT_DOMAIN_CONTROLLER Then sOS = sOS & " Domain Controller" ElseIf .wProductType = VER_NT_WORKSTATION Then sOS = sOS & " Workstation" End If End If ´get version/build no sOS = sOS & " Version " & .dwMajorVersion & "." & .dwMinorVersion & " " & StripTerminator(.szCSDVersion) & " (Build " & .dwBuildNumber & ")" ElseIf .dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then ´get minor version info If .dwMinorVersion = 0 Then sOS = "Microsoft Windows 95" ElseIf .dwMinorVersion = 10 Then sOS = "Microsoft Windows 98" ElseIf .dwMinorVersion = 90 Then sOS = "Microsoft Windows Millenium" Else sOS = "Microsoft Windows 9?" End If ´get version/build no sOS = sOS & "Version " & .dwMajorVersion & "." & .dwMinorVersion & " " & StripTerminator(.szCSDVersion) & " (Build " & .dwBuildNumber & ")" End If End With GetVersionInfo = sOS End Function Private Function StripTerminator(sString As String) As String StripTerminator = Left$(sString, InStr(sString, Chr$(0)) - 1) End Function
很麻烦是吗?是的,很,非常麻烦,那有什么好一点的办法吗?是的有,你看看
我保证你可以在System的命名空间中找到Environment和Version这两个类
你看通过名字你也猜的到,它们的作用一下是Version中的公共成员用来记录
版本信息 Build 获取此实例版本号的内部版本号部分的值。 Major 获取此实例版本号的主版本号部分的值。 Minor 获取此实例版本号的次版本号部分的值。 Revision 获取此实例版本号的修订号部分的值。
那OS呢?别忘了Environment它,Environment.OSVersion它的这个静态成员
正是用来解决问题的. |