Programmatically Determine the Operating System using .NET

Here’s how to programmatically determine the Windows OS you are using. I will make use of the ManagementObjectSearcher class here


using System;
using System.Management;

namespace ConsoleApplication1
class Program
public static void Main()
string winos = "Select Name from Win32_OperatingSystem";
ManagementObjectSearcher mos =
new ManagementObjectSearcher(winos);
foreach (ManagementObject mo in mos.Get())
Console.WriteLine("OS Name: {0}", mo["Name"]);


Imports System
Imports System.Management

Namespace ConsoleApplication1
Friend Class Program
Public Shared Sub Main()
Dim winos As String = "Select Name from Win32_OperatingSystem"
Dim mos As New ManagementObjectSearcher(winos)
For Each mo As ManagementObject In mos.Get()
Console.WriteLine("OS Name: {0}", mo("Name"))
Next mo
End Sub
End Class
End Namespace

Here the Win32_OperatingSystem WMI class represents a Windows-based operating system installed on a computer.



Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: