Operating system info and processor info in c#
Hi…
In this blog I will tell you that how to display operating
system info and processor info by using c#. We can take help of WMI classes to display
information of processor as well as to display information of operating system.
There is another way to perform this task which is more efficient than WMI
classes that is by using Registry editor. We know that Registry editor contains
all information of system as well processor in their database. Then we can use
registry editor to retrieve these values.
In this blog I will use concept, Registry as well as use WMI
class. I will use Registry editor to display processor information and use WMI
classes to display operating system information. Here is sample program which
will complete out task..
Program to display operating system and processor info
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Management; //This namespace is used to work with WMI classes. For
using this namespace add reference of System.Management.dll .
using
Microsoft.Win32; //This namespace is used to work with Registry editor.
namespace
OperatingSystemInfo1
{
class TestProgram
{
static void Main(string[]
args)
{
SystemInfo
si = new SystemInfo(); //Create an
object of SystemInfo class.
si.getOperatingSystemInfo(); //Call
get operating system info method which will display operating system
information.
si.getProcessorInfo(); //Call
get processor info method which will
display processor info.
Console.ReadLine(); //Wait
for user to accept input key.
}
}
public class SystemInfo
{
public void getOperatingSystemInfo()
{
Console.WriteLine("Displaying operating system info....\n");
//Create
an object of ManagementObjectSearcher class and pass query as parameter.
ManagementObjectSearcher
mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
foreach
(ManagementObject managementObject in mos.Get())
{
if
(managementObject["Caption"] != null)
{
Console.WriteLine("Operating System Name :
" + managementObject["Caption"].ToString()); //Display
operating system caption
}
if
(managementObject["OSArchitecture"]
!= null)
{
Console.WriteLine("Operating System Architecture :
" + managementObject["OSArchitecture"].ToString()); //Display
operating system architecture.
}
if
(managementObject["CSDVersion"] !=
null)
{
Console.WriteLine("Operating System Service Pack :
" + managementObject["CSDVersion"].ToString()); //Display
operating system version.
}
}
}
public void getProcessorInfo()
{
Console.WriteLine("\n\nDisplaying Processor Name....");
RegistryKey
processor_name = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0",
RegistryKeyPermissionCheck.ReadSubTree); //This registry
entry contains entry for processor info.
if
(processor_name != null)
{
if
(processor_name.GetValue("ProcessorNameString")
!= null)
{
Console.WriteLine(processor_name.GetValue("ProcessorNameString")); //Display
processor ingo.
}
}
}
}
}
Output of following code snippet is as follows
Displaying operating system info....
Operating System Name
: Microsoft Windows 7 Ultimate
Operating System Architecture :
64-bit
Operating System Service Pack :
Service Pack 1
Displaying Processor Name....
Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz