Windows version in c# [duplicate]
Asked Answered
E

4

10

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

I have tried using

OperatingSystem os = Environment.OSVersion;

Version ver = os.Version;

But the result is

Plataform: WIN32NT

version 6.2.9200

Version minor: 2

Version Major: 6

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

How can I detect it?

Thanks

Extradition answered 12/2, 2014 at 19:42 Comment(2)
Operating system versions: msdn.microsoft.com/en-us/library/windows/desktop/…Arium
Yeah. Non-issue. Someone just does not know that windows internal numbering is different from the marketing name.Cartogram
M
14

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

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

  • Windows Server 2016 & 2019 - 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 Edition - 5.2
  • Windows XP - 5.1
  • Windows 2000 - 5.0

*For applications that have been manifested for Windows 8.1 or 10. Applications not manifested for 8.1 / 10 will return the Windows 8 OS version value (6.2).

Here's the source.

Also, from the same source:

Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using the Version API Helper functions to determine the operating system platform or version number, test for the presence of the feature itself.

Mady answered 12/2, 2014 at 19:45 Comment(1)
+1 for the warning about testing for the feature itself. This made me think about a different way to solve my problem that will avoid the pain of dealing with version numbers (I'm simply going to catch the error thrown when the feature is not there, and handle things from there).Counterreply
B
5

In my scenario I needed my application to capture computer info for possible bug-reports and statistics.

I did not find the solutions where an application manifest had to be added satisfactory. Most of the suggestions I found while googling this suggested just that, unfortunately.

Thing is, when using a manifest, each OS version has to be added manually to it in order for that particular OS version to be able to report itself at runtime.

In other words, this becomes a race condition: A user of my app may very well be using a version of my app that pre-dates the OS in use. I would have to upgrade the app immediately when a new OS version was launched by Microsoft. I would also have to force the users to upgrade the app at the same time as they updated the OS.

In other words, not very feasible.

After browsing through the options I found some references (surprisingly few compared to the app manifest) that instead suggested using registry lookups.

My (chopped down) ComputerInfo class with only WinMajorVersion, WinMinorVersion and IsServer properties looks like this:

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;
            }
        }
    }
}
Bunko answered 9/6, 2016 at 3:25 Comment(0)
J
3

I released the OsInfo nuget to easily compare Windows versions.

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+
Judie answered 22/1, 2015 at 17:54 Comment(3)
have you any plans to add Windows 10 support?Blubber
Please create an issue on github and discuss it there. I'm sure that is easily added. If you send a pull request I'll at least merge it in and release a new nuget.Judie
Thanks for the reply - raised an issue - github.com/anjdreas/OsInfo/issues/1Blubber
M
2

Try this:

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: https://mcmap.net/q/139085/-how-to-get-the-quot-friendly-quot-os-version-name

Metropolitan answered 23/3, 2016 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.