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
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 ). UBR: Actual patch level (LCU). ProductName: For humans. This one's not always authoritative for filtering purposes.
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?
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.
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.
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.
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.
It will work as long as your app manifest contain the necessary supportedOS https://learn.microsoft.com/en-us/windows/win32/sysinfo/targeting-your-application-at-windows-8-1
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.