27

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?

CDspace
  • 2,639
  • 18
  • 30
  • 36
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 1
    possible duplicate of [How to get the "friendly" OS Version Name?](http://stackoverflow.com/questions/577634/how-to-get-the-friendly-os-version-name) – Mat Jun 13 '11 at 14:34
  • see this answer http://stackoverflow.com/questions/577634/how-to-get-the-friendly-os-version-name/2016557#2016557 – Bala R Jun 13 '11 at 14:34
  • Check out http://stackoverflow.com/questions/860459/determine-os-using-the-environment-osversion-object-c – Brandon E Taylor Jun 13 '11 at 14:36
  • Possible duplicate of [how do I detect user operating system](http://stackoverflow.com/questions/9734668/how-do-i-detect-user-operating-system) – Offir Aug 08 '16 at 12:10

8 Answers8

60

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;
}
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • 1
    Some users of my software are getting an `UnauthorizedAccessException` in `ManagementObjectSearcher.Get()` when running my software. Any idea why that might be? – Walt D May 13 '16 at 03:19
  • @WaltD open a question with that, and include in that question a link to this answer. – ANeves Sep 05 '16 at 14:30
  • According to the wording in [MSDN](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.computerinfo.osfullname(v=vs.110).aspx), `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. – ANeves Sep 05 '16 at 14:32
  • 1
    @ANeves I did, [here](http://stackoverflow.com/questions/37200906/why-does-managementobjectsearcher-get-throw-an-unauthorizedaccessexception), but no one answered it. – Walt D Sep 06 '16 at 00:35
12

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 "";
    }
domskey
  • 1,102
  • 11
  • 16
9

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.

ANeves
  • 6,219
  • 3
  • 39
  • 63
George
  • 295
  • 4
  • 7
  • 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`. – ElektroStudios Jun 23 '15 at 08:36
  • @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." – George Jun 24 '15 at 18:23
  • How-to know if (WMI) is installed on the computer ? – Kiquenet Jan 16 '19 at 16:21
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

Community
  • 1
  • 1
3
System.OperatingSystem osInfo = System.Environment.OSVersion;
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
  • 3
    it's giving "Microsoft Windows NT 6.2.9200.0" for Win 10 PC. – SaddamBinSyed Jul 28 '19 at 11:09
  • @SaddamBinSyed, add an `app.manifest` XML file to your application and uncomment the `` 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` – Gabor Nov 18 '21 at 02:21
2
public int OStype()
    {
        int os = 0;
        IEnumerable<string> list64 = Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\SysWOW64"));
        IEnumerable<string> list32 = Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\System32"));
        if (list32.Count() > 0)
        {
            os = 32;
            if (list64.Count() > 0)
                os = 64;
        }
        return os;
    }
1

Though this is not a fully C# way of detecting the OS Name, below code works for my needs-

public static string GetFriendlyOSName()
    {
        System.Diagnostics.Process cmd = new System.Diagnostics.Process();
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.Arguments = "/C systeminfo | findstr /c:\"OS Name\"";
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.CreateNoWindow = true;
        cmd.Start();
        cmd.WaitForExit();
        string output = cmd.StandardOutput.ReadToEnd();
        cmd.Close();
        return output.Split(new[] { ':' }, 2)[1].Trim();
    }

Note:

  1. This code works for only Windows OS.
  2. The code is tested only on Windows 10, 11.
kuroko09
  • 11
  • 1
  • `OS Name` will work only on English Operation Systems. For e.g. German it would be `Betriebssystemname`. – KargWare Nov 10 '22 at 11:48
-1
string text = (string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");

this code will get the full os name like this "Windows 8.1 Pro"