Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the "friendly" OS Version Name?

I am looking for an elegant way to get the OS version like: "Windows XP Professional Service Pack 1" or "Windows Server 2008 Standard Edition" etc.

Is there an elegant way of doing that?

I am also interested in the processor architecture (like x86 or x64).

like image 590
Stefan Koell Avatar asked Feb 23 '09 13:02

Stefan Koell


People also ask

How do I find my OS version using CMD?

Press [Windows] key + [R] to open the “Run” dialog box. Enter cmd and click [OK] to open Windows Command Prompt. Type systeminfo in the command line and hit [Enter] to execute the command.


2 Answers

You can use WMI to get the product name ("Microsoft® Windows Server® 2008 Enterprise "):

using System.Management; var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>()                       select x.GetPropertyValue("Caption")).FirstOrDefault(); return name != null ? name.ToString() : "Unknown"; 
like image 122
Sean Kearon Avatar answered Oct 05 '22 11:10

Sean Kearon


You should really try to avoid WMI for local use. It is very convenient but you pay dearly for it in terms of performance. This is quick and simple:

    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 "";     } 
like image 42
domskey Avatar answered Oct 05 '22 11:10

domskey