How to add code to check OS version in my programming projects

Discussion in 'Mixed Languages' started by steven4554, Aug 5, 2024.

  1. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    I wonder if someone can explain how to add code that checks for the Windows OS version and also prevent it from running on unsupported OS versions, please.

    I am using MS .NET Framework WinForms in Visual Studio Community 2022 v11.0 Preview 6
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. 12 lb Turkey

    12 lb Turkey MDL Member

    Nov 24, 2022
    120
    64
    10
    Basically you parse

    [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion]
    CurrentMajorVersionNumber
    CurrentMinorVersionNumber
    CurrentBuildNumber
    UBR
    ProductName

    From PowerShell:
    Code:
    $RegKey = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'
    
    '{0}.{1}.{2}.{3} - {4}' -f $RegKey.CurrentMajorVersionNumber, $RegKey.CurrentMinorVersionNumber, $RegKey.CurrentBuildNumber, $RegKey.UBR, $RegKey.ProductName
    
    Code:
    10.0.19045.4170 - Windows 10 Pro
    
    This output matches winver.

    Major number: For W10 or 11, it's always 10. W7 = 6.1, W8 = 6.2, W8.1 = 6.3
    Minor number: For W10 or 11, it's always 0.
    Build number. Presumably everyone has access to a table (or ask abbodi :rolleyes:).
    UBR: Actual patch level (LCU).
    ProductName: For humans. This one's not always authoritative for filtering purposes.
     
  3. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    So i just enter that into code in my projects, and it should detect the OS version? Also how to enter code to stop program from running on unsupported Windows versions?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. 12 lb Turkey

    12 lb Turkey MDL Member

    Nov 24, 2022
    120
    64
    10
    What's considered an unsupported Windows version? Can you provide an example, no W10 21H2 or W11 24H2? or nothing below 22621.1234?

    You would do a conditional check, throw a warning for the user, and exit your app.
     
  5. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    Only support Windows 11 v22H2 or above.

    Yes that's what i would like to programme into my projects, that it checks for the minimum OS version which would be 10.0.22621.1 and if it doesn't find that build version then it would prompt the user a message about them running a unsupported version of windows. Then the user click on OK to exit the program.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. 12 lb Turkey

    12 lb Turkey MDL Member

    Nov 24, 2022
    120
    64
    10
    I don't program in .NET, but System.Environment.OSVersion will return the Build (ie. 22621) as an int for comparison. Then you can do >= 22621.
     
  7. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    Thanks, I'll do some further research and try that. :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    #8 steven4554, Oct 1, 2024
    Last edited: Oct 7, 2024
    (OP)
    Update

    Using AI MS's Co-pilot I was finally able to add Windows OS check and stop my projects from running on any version below Windows 11 v22H2. Unfortunately, this code uses a old way to detect the Windows OS and will not work correctly on Windows 11. Anyway here's the code I used below to help anyone else:-
    Code:
    Imports System
    Imports System.Runtime.InteropServices
    
    Public Class Form1 Name
        Private Sub Form1_MainMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If Not IsSupportedOS() Then
                MessageBox.Show("This application requires Windows 11 version 22H2 or later.", "Unsupported OS", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Application.Exit()
            End If
        End Sub
    
        Private Function IsSupportedOS() As Boolean
            Dim osVersion As Version = System.Environment.OSVersion.Version
            Dim major As Integer = osVersion.Major
            Dim minor As Integer = osVersion.Minor
            Dim build As Integer = osVersion.Build
    
            ' Windows 11 version 22H2 has a major version of 10, minor version of 0, and build number 22621 or higher
            Return major = 10 AndAlso minor = 0 AndAlso build >= 22621
        End Function
    End Class
    
    Replace Form1 Name with what you named your Main Menu form.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    17,186
    90,642
    340
  10. steven4554

    steven4554 MDL Expert

    Jul 12, 2009
    1,499
    2,800
    60
    #10 steven4554, Oct 2, 2024
    Last edited: Oct 7, 2024
    (OP)
    Thanks abbodi1406 :)

    Update - Finally able to get it working in my .NET Framework 4.8.1 projects. As I'm currently running the latest Canary build 27718, I had modify my code to target a much higher build number to get the error message for confirmation. So will apply this to my other project now.

    Again thanks to those who helped me with this.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...