Interacting with the Hardware using C#

Get OS, Service Pack Information
 
Use the System.Environment.OSVersion
  1. OperatingSystem os = System.Environment.OSVersion;  
  2. Console.WriteLine("Platform: {0}", os.Platform);  
  3. Console.WriteLine("Service Pack: {0}", os.ServicePack);  
  4. Console.WriteLine("Version: {0}", os.Version);  
  5. Console.WriteLine("VersionString: {0}", os.VersionString);  
  6. 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
  1. using System;  
  2. using System.Management;  
  3. using System.Windows.Forms;  
  4. namespace HardwareInfo  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Console.WriteLine("Machine: {0}", Environment.MachineName);  
  11.             Console.WriteLine("# of processors (logical): {0}", Environment.ProcessorCount);  
  12.             Console.WriteLine("# of processors (physical): {0}", CountPhysicalProcessors());  
  13.             Console.WriteLine("RAM installed: {0:N0} bytes", CountPhysicalMemory());  
  14.             Console.WriteLine("Is OS 64-bit? {0}", Environment.Is64BitOperatingSystem);  
  15.             Console.WriteLine("Is process 64-bit? {0}", Environment.Is64BitProcess);  
  16.             Console.WriteLine("Little-endian: {0}", BitConverter.IsLittleEndian);  
  17.             foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)  
  18.             {  
  19.                 Console.WriteLine("Screen {0}", screen.DeviceName);  
  20.                 Console.WriteLine("\tPrimary {0}", screen.Primary);  
  21.                 Console.WriteLine("\tBounds: {0}", screen.Bounds);  
  22.                 Console.WriteLine("\tWorking Area: {0}", screen.WorkingArea);  
  23.                 Console.WriteLine("\tBitsPerPixel: {0}", screen.BitsPerPixel);  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.         private static UInt32 CountPhysicalProcessors()  
  28.         {  
  29.             //you must add a reference to the System.Management assembly  
  30.             ManagementObjectSearcher objects =  
  31.             new ManagementObjectSearcher(  
  32.             "SELECT * FROM Win32_ComputerSystem");  
  33.             ManagementObjectCollection coll = objects.Get();  
  34.             foreach (ManagementObject obj in coll)  
  35.             {  
  36.                 return (UInt32)obj["NumberOfProcessors"];  
  37.             }  
  38.             return 0;  
  39.         }  
  40.         private static UInt64 CountPhysicalMemory()  
  41.         {  
  42.             ManagementObjectSearcher objects =  
  43.             new ManagementObjectSearcher(  
  44.             "SELECT * FROM Win32_PhysicalMemory");  
  45.             ManagementObjectCollection coll = objects.Get();  
  46.             UInt64 total = 0;  
  47.             foreach (ManagementObject obj in coll)  
  48.             {  
  49.                 total += (UInt64)obj["Capacity"];  
  50.             }  
  51.             return total;  
  52.         }  
  53.     }  
  54. }  


Similar Articles