Get OS Version / Friendly Name in C#

47,840

Solution 1

Add a reference and using statements for System.Management, then:

public static string GetOSFriendlyName()
{
    string result = string.Empty;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
    foreach (ManagementObject os in searcher.Get())
    {
        result = os["Caption"].ToString();
        break;
    }
    return result;
}

Solution 2

You should really try to avoid WMI for local use. It is very convenient but you pay dearly for it in terms of performance. Think laziness tax!

Kashish's answer about the registry does not work on all systems. Code below should and also includes the service pack:

    public string HKLM_GetString(string path, string key)
    {
        try
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(path);
            if (rk == null) return "";
            return (string)rk.GetValue(key);
        }
        catch { return ""; }
    }
    public string FriendlyName()
    {
        string ProductName = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName");
        string CSDVersion = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CSDVersion");
        if (ProductName != "")
        {
            return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName +
                        (CSDVersion != "" ? " " + CSDVersion : "");
        }
        return "";
    }

Solution 3

Add a .NET reference to Microsoft.VisualBasic. Then call:

new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName

From MSDN:

This property returns detailed information about the operating system name if Windows Management Instrumentation (WMI) is installed on the computer. Otherwise, this property returns the same string as the My.Computer.Info.OSPlatform property, which provides less detailed information than WMI can provide.information than WMI can provide.

Solution 4

String subKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion";
RegistryKey key = Registry.LocalMachine;
RegistryKey skey = key.OpenSubKey(subKey);
Console.WriteLine("OS Name: {0}", skey.GetValue("ProductName"));

I hope that you find this useful

Solution 5

System.OperatingSystem osInfo = System.Environment.OSVersion;
Share:
47,840

Related videos on Youtube

Boardy
Author by

Boardy

Develop apps and services in PHP, C#, C++, HTML, CSS, Jquery etc, recently started learning React.

Updated on July 09, 2022

Comments

  • Boardy
    Boardy 6 months

    I am currently working on a C# project. I want to collect users statistics to better develop the software. I am using the Environment.OS feature of C# but its only showing the OS name as something like Microsoft Windows NT

    What I want to be able to retrieve is the actual known name of the OS like whether it is Windows XP, Windows Vista or Windows 7 and etc.

    Is this possible?

  • ElektroStudios
    ElektroStudios over 7 years
    Is a wrong solution, in my case it just returns the string "Microsoft". is a wrong solution, this is the same as: My.Computer.Info.OSFullName.
  • George over 7 years
    @ElektroStudios: See MSDN: "This property returns detailed information about the operating system name if Windows Management Instrumentation (WMI) is installed on the computer. Otherwise, this property returns the same string as the My.Computer.Info.OSPlatform property, which provides less detailed information than WMI can provide."
  • kostas.kapasakis over 6 years
    Some users of my software are getting an UnauthorizedAccessException in ManagementObjectSearcher.Get() when running my software. Any idea why that might be?
  • ANeves
    ANeves over 6 years
    @WaltD open a question with that, and include in that question a link to this answer.
  • ANeves
    ANeves over 6 years
    According to the wording in MSDN, if (WMI) is installed on the computer (if!), it's possible that some computers don't have WMI installed. For those computers, this approach will likely fail.
  • kostas.kapasakis over 6 years
    @ANeves I did, here, but no one answered it.
  • Scott about 6 years
    The registry key will be slightly different for 64-bit vs 32-bit as Wow6432Node is not present in the Windows 7 32-bit registry for example.
  • Kiquenet
    Kiquenet almost 4 years
    How-to know if (WMI) is installed on the computer ?
  • SaddamBinSyed
    SaddamBinSyed over 3 years
    it's giving "Microsoft Windows NT 6.2.9200.0" for Win 10 PC.
  • Gabor about 1 year
    @SaddamBinSyed, add an app.manifest XML file to your application and uncomment the <supportedOS> line for Windows 10 under the assembly\compatibility\application XML node. Then you will get the actual version string, e.g. Microsoft Windows NT 10.0.19043.0
  • Mister Cook
    Mister Cook 9 months
    On Windows 11 this will report, e.g. "Windows 10 Enterprise"
  • Mister Cook
    Mister Cook 9 months
    On Windows 11 ProductName is "Windows 10"