Get the OS version information in .NET

zhaozj2021-02-16  50

Easily get the version number of the OS (small discussion) in .NET.

If you don't do it, you want to write something. In fact, it is not very decent, I believe who will

Who will, why do I want to write? Fiscalism is nothing. OK, start, first

Let's introduce an API function. GetversionEx, this function is used to return one

Structure OVSVersionInfo This structure is used to record version information. The following is a VB code

Option expedition

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

DWOSVERSIONFOSIZE 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 Os "InfoEx) 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.dwosveionInfosize = 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

Very trouble? Yes, very, very trouble, what is a good way? Yes, you have

I guarantee that you can find two classes of Environment and Version in System's namespace.

You can also guess through the name, and their role is that the public members in Version are used to record

Version Information

Build Gets the value of the internal version number of this instance version number.

Major gets the value of the main version number of this instance version number.

Minor gets the value of the secondary version of this instance version number.

Revision gets the value of the revision number portion of this instance version number.

OS? Don't forget Environment.OSversion, this static member

It is used to solve problems.

The following is the OS version reference table:

Platformidmajor Version

Minor Version

Operating system

Win32windows

> = 4

0

Win95

Win32windows

> = 4

> 0 && <90

Win98

Win32windows

> = 4

> 0 &&> = 90

WinMe

Win32NT

<= 4

0

Winnt

Win32NT 5

5

0

Win2k

Win32NT 5

5

> 0

WinXP

Ok, I'm finished, it is estimated that I am ready. Come on.

转载请注明原文地址:https://www.9cbs.com/read-27452.html

New Post(0)