Get OS Version / Friendly Name in C#
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;
Related videos on Youtube

Boardy
Develop apps and services in PHP, C#, C++, HTML, CSS, Jquery etc, recently started learning React.
Updated on July 09, 2022Comments
-
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 NTWhat 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?
-
Mat over 11 yearspossible duplicate of How to get the "friendly" OS Version Name?
-
Bala R over 11 yearssee this answer stackoverflow.com/questions/577634/…
-
Brandon E Taylor over 11 yearsCheck out stackoverflow.com/questions/860459/…
-
Offir over 6 yearsPossible duplicate of how do I detect user operating system
-
-
ElektroStudios over 7 yearsIs 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 yearsSome users of my software are getting an
UnauthorizedAccessException
inManagementObjectSearcher.Get()
when running my software. Any idea why that might be? -
ANeves over 6 years@WaltD open a question with that, and include in that question a link to this answer.
-
ANeves over 6 yearsAccording 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 yearsThe 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 almost 4 yearsHow-to know if (WMI) is installed on the computer ?
-
SaddamBinSyed over 3 yearsit'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 theassembly\compatibility\application
XML node. Then you will get the actual version string, e.g.Microsoft Windows NT 10.0.19043.0
-
Mister Cook 9 monthsOn Windows 11 this will report, e.g. "Windows 10 Enterprise"
-
Mister Cook 9 monthsOn Windows 11 ProductName is "Windows 10"