Windows version in C #

I want to know which version of Windows has a PC .. in C # Framework 3.5

I tried using

OS os = Environment.OSVersion;

Version ver = os.Version;

But the result

Plataform: WIN32NT

version 6.2.9200

Minor version: 2

Version: 6

The problem is that I have "Windows 8 Pro" ...

How can I detect it?

thanks

+8
c # operating-system
source share
4 answers

You will have to match the version numbers with the corresponding string value yourself.

Here is a list of the most recent Windows OS and their version number:

  • Windows Server 2016 Technical Preview - 10.0 *
  • Windows 10 - 10.0 *
  • Windows 8.1 - 6.3 *
  • Windows Server 2012 R2 - 6.3 *
  • Windows 8 - 6.2
  • Windows Server 2012 - 6.2
  • Windows 7 - 6.1
  • Windows Server 2008 R2 - 6.1
  • Windows Server 2008 - 6.0
  • Windows Vista - 6.0
  • Windows Server 2003 R2 - 5.2
  • Windows Server 2003 - 5.2
  • Windows XP 64-bit - 5.2
  • Windows XP - 5.1
  • Windows 2000 - 5.0

* For applications that were shown for Windows 8.1 or 10. Applications that are not displayed for 8.1 / 10 will return the value of the version of Windows 8 (6.2).

Here is the source .

Also from the same source:

Identifying the current operating system is usually not the best way to determine if a specific function of the operating system is available. This is because new features in the redistributable DLL may be added to the operating system. Instead of using the Version of the Function Assistant API to determine the platform or operating system version number, check for the presence of the function itself.

+12
source share

In my scenario, I needed an application to capture information about a computer to receive possible error reports and statistics.

I did not find solutions in which the application manifest was to be added. Most of the offers that I found while searching on Google offered exactly that, unfortunately.

The fact is that when using the manifest, each OS version must be added manually so that this OS version can communicate itself at runtime.

In other words, it becomes a race condition. A user of my application can very well use the version of my application that precedes the OS used. I would need to update the application immediately when Microsoft will release a new version of the OS. I also have to get users to update the application while updating the OS.

In other words, not very doable .

After looking at the parameters, I found several links (surprisingly few compared to the application manifest), which instead suggested using a registry search.

My ComputerInfo class (chopped off) with the WinMajorVersion , WinMinorVersion and IsServer as follows:

 using Microsoft.Win32; namespace Inspection { /// <summary> /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup. /// </summary> public static class ComputerInfo { /// <summary> /// Returns the Windows major version number for this computer. /// </summary> public static uint WinMajorVersion { get { dynamic major; // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10, // and will most likely (hopefully) be there for some time before MS decides to change this - again... if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major)) { return (uint) major; } // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion' dynamic version; if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version)) return 0; var versionParts = ((string) version).Split('.'); if (versionParts.Length != 2) return 0; uint majorAsUInt; return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0; } } /// <summary> /// Returns the Windows minor version number for this computer. /// </summary> public static uint WinMinorVersion { get { dynamic minor; // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10, // and will most likely (hopefully) be there for some time before MS decides to change this - again... if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber", out minor)) { return (uint) minor; } // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion' dynamic version; if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version)) return 0; var versionParts = ((string) version).Split('.'); if (versionParts.Length != 2) return 0; uint minorAsUInt; return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0; } } /// <summary> /// Returns whether or not the current computer is a server or not. /// </summary> public static uint IsServer { get { dynamic installationType; if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType", out installationType)) { return (uint) (installationType.Equals("Client") ? 0 : 1); } return 0; } } private static bool TryGeRegistryKey(string path, string key, out dynamic value) { value = null; try { var rk = Registry.LocalMachine.OpenSubKey(path); if (rk == null) return false; value = rk.GetValue(key); return value != null; } catch { return false; } } } } 
+5
source share

I released OsInfo nuget to easily compare versions of Windows.

 bool win8OrLess = Environment.OSVersion.IsLessThanOrEqualTo(OsVersion.Win8); bool winXp = Environment.OSVersion.IsEqualTo(OsVersion.WinXP); int? servicePack = Environment.OSVersion.GetServicePackVersion(); bool is64bit = Environment.OSVersion.Is64Bit(); // Already covered in .NET 4.5+ 
+2
source share

Try the following:

 using System.Management; private string fnGetFriendlyName() { var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).FirstOrDefault(); return name != null ? name.ToString() : "Unknown"; } 

Source: stack overflow

+2
source share

All Articles