Quantcast Programmatically determine the server information using .net

Programmatically determine the server information using .net

by Neon Quach 7. October 2010 21:35

In this blog, I will give you a snippet code which can get the Window information using .net.

using System;
using System.Management;
 
namespace CS
{
    class Program
    {
        static void Main()
        {
            ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher query = new ManagementObjectSearcher(oq);
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                Console.WriteLine((string)mo["Caption"]);
                Console.WriteLine((string)mo["Version"]);
                Console.WriteLine((string)mo["Manufacturer"]);
                Console.WriteLine((string)mo["WindowsDirectory"]);
                Console.WriteLine((string)mo["SerialNumber"]);
                Console.WriteLine(Environment.Version.ToString());
            }
            Console.Read();
        }
    }
}

 
VB:

Imports System.Management

Module Module1
    Sub Main()
        Dim oq As New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
        Dim query As New ManagementObjectSearcher(oq)
        Dim queryCollection = query.Get()
        For Each mo As ManagementObject In queryCollection
            Console.WriteLine(DirectCast(mo("Caption"), String))
            Console.WriteLine(DirectCast(mo("Version"), String))
            Console.WriteLine(DirectCast(mo("Manufacturer"), String))
            Console.WriteLine(DirectCast(mo("WindowsDirectory"), String))
            Console.WriteLine(DirectCast(mo("SerialNumber"), String))
            Console.WriteLine(Environment.Version.ToString())
        Next
        Console.Read()
    End Sub
End Module

To run this code, please make sure that you have included System.Management .dll in project Reference.


Output:
Microsoft Windows 7 Ultimate
6.1.7600
Microsoft Corporation
C:\Windows
00426-OEM-8992662-00006
2.0.50727.4927

Hope this help,

Tags: ,


Categories: c# | vb.net

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers