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

C#

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"]);
}
Console.ReadLine();
}
}
}

VB.NET

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
Console.ReadLine()
End Sub
End Class
End Namespace

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

OUTPUT

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: