Check OS best practice

OSVBNET 1,386 Reputation points
2022-04-13T17:47:14.08+00:00

Hello
To check the OS version and terminate the app if OS is older than XP (in .NET framework 4.0):

If Not Version.op_GreaterThanOrEqual(Environment.OSVersion.Version, New Version(5, 1, 0, 0)) Then...

I've seen that should not use Environment, which method you guys recommend?

** I mean a more reliable method to detect OS version in .net 4.0

** In .NET versions prior to .NET 5, Environment.OSVersion returns an OS version that may be incorrect when an application runs under Windows compatibility mode

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,636 Reputation points
    2022-04-13T19:24:08.723+00:00

    The method used by MS up to Windows 10 is RtlGetVersion
    (then comparing Major/Minor from Operating System Version)
    (on Windows 11, APIs from Winbrand.dll)

    A test :

            Dim ovi As RTL_OSVERSIONINFOW = New RTL_OSVERSIONINFOW With {  
                .dwOSVersionInfoSize = Marshal.SizeOf(GetType(RTL_OSVERSIONINFOW))  
            }  
            Dim ntStatus As UInteger = RtlGetVersion(ovi)  
            Console.WriteLine("Major version : {0} - Minor version : {1} - Build Number : {2}", ovi.dwMajorVersion.ToString(),  
                              ovi.dwMinorVersion.ToString(), ovi.dwBuildNumber.ToString())  
    

    with :

        <DllImport("Ntdll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
        Public Shared Function RtlGetVersion(ByRef lpVersionInformation As RTL_OSVERSIONINFOW) As UInteger  
        End Function  
      
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>  
        Public Structure RTL_OSVERSIONINFOW  
            Public dwOSVersionInfoSize As UInteger  
            Public dwMajorVersion As UInteger  
            Public dwMinorVersion As UInteger  
            Public dwBuildNumber As UInteger  
            Public dwPlatformId As UInteger  
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)>  
            Public szCSDVersion As String  
        End Structure  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-04-13T17:56:58.983+00:00

    ............ following the 'Then', maybe the keyword 'End' is what you are looking for - maybe not.


  2. Michael Taylor 47,966 Reputation points
    2022-04-13T18:43:17.73+00:00

    There is nothing wrong with using Environment.OSVersion.Version to check for a pre-XP OS.

    But I think this is a mute point because if your app is targeting .NET 4 then the earliest OS you can install the framework on is XP if I remember correctly. Without the framework your app won't run. Hence you cannot run your app on a version of the OS prior to XP anyway so this shouldn't be a scenario you need to worry about.