SIGN UP MEMBER LOGIN:    
ARTICLE

Interacting with the Hardware using C# 4.0

Posted by Biswa Pujarini Mohapatra Articles | Hardware June 03, 2010
In this article you will know how to get OS, Service Pack, CLR Version, CPU and other hardware Information using C#.
Reader Level:

Get OS, Service Pack Information

Use the System.Environment.OSVersion


OperatingSystem os = System.Environment.OSVersion;
Console.WriteLine("Platform: {0}", os.Platform);
Console.WriteLine("Service Pack: {0}", os.ServicePack);
Console.WriteLine("Version: {0}", os.Version);
Console.WriteLine("VersionString: {0}", os.VersionString);
Console.WriteLine("CLR Version: {0}", System.Environment.Version); 

This produces the following output (on my Windows 7 system):

Platform: Win32NT
Service Pack:
Version: 6.1.7600.0
VersionString: Microsoft Windows NT 6.1.7600.0
CLR Version: 4.0.21006.1

The version information is in a struct, so you can make decisions based on version or subversion values.

Get CPU and Other Hardware Information

using System;
using System.Management;
using System.Windows.Forms;
namespace HardwareInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Machine: {0}", Environment.MachineName);
            Console.WriteLine("# of processors (logical): {0}", Environment.ProcessorCount);
            Console.WriteLine("# of processors (physical): {0}", CountPhysicalProcessors());
            Console.WriteLine("RAM installed: {0:N0} bytes", CountPhysicalMemory());
            Console.WriteLine("Is OS 64-bit? {0}", Environment.Is64BitOperatingSystem);
            Console.WriteLine("Is process 64-bit? {0}", Environment.Is64BitProcess);
            Console.WriteLine("Little-endian: {0}", BitConverter.IsLittleEndian);
            foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
            {
                Console.WriteLine("Screen {0}", screen.DeviceName);
                Console.WriteLine("\tPrimary {0}", screen.Primary);
                Console.WriteLine("\tBounds: {0}", screen.Bounds);
                Console.WriteLine("\tWorking Area: {0}", screen.WorkingArea);
                Console.WriteLine("\tBitsPerPixel: {0}", screen.BitsPerPixel);
            }
            Console.ReadKey();
        }
        private static UInt32 CountPhysicalProcessors()
        {
            //you must add a reference to the System.Management assembly
            ManagementObjectSearcher objects =
            new ManagementObjectSearcher(
            "SELECT * FROM Win32_ComputerSystem");
            ManagementObjectCollection coll = objects.Get();
            foreach (ManagementObject obj in coll)
            {
                return (UInt32)obj["NumberOfProcessors"];
            }
            return 0;
        }
        private static UInt64 CountPhysicalMemory()
        {
            ManagementObjectSearcher objects =
            new ManagementObjectSearcher(
            "SELECT * FROM Win32_PhysicalMemory");
            ManagementObjectCollection coll = objects.Get();
            UInt64 total = 0;
            foreach (ManagementObject obj in coll)
            {
                total += (UInt64)obj["Capacity"];
            }
            return total;
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor