Welcome to Ultra Developers.

the company that developes your success

Read Blog


How to: Get your Machine Processor Id using C#

How to: Get your Machine Processor Id using C#

Sometimes you need to store a piece of information that uniquely identifies the machine your application runs on. One of the information that uniquely identifies the machine is the Processor Id. In this article we will create an application that retrieves the machine Processor Id.

In this article we use the Windows Management Instrumentation. For more information about Windows Management Instrumentation see the following MSDN section http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx

Using the Code:

To create an application that retrieves the machine Processor Id follow the following steps:

  1. Create a new windows application using Visual Studio 2005/2008/2010.
  2. Rename Form1 to ProcessorForm.
  3. Add a ToolStrip control to the ProcessorForm and rename it to ProcessorToolStrip.
  4. Add a ToolStripButton to the ProcessorToolStrip and rename it to ProcessorToolStripButton and set its Text property to Processor.
  5. Right click the References under your project and select Add Reference. Select .NET Tab and find the System.Management namespace and click OK.

Adding Reference

  1. Import the System.Management namespace using the following statement:
using System.Management;
  1. The System.Management namespace provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure. Applications and services can query for interesting management information, using classes derived from ManagementObjectSearcher and ManagementQuery, or subscribe to a variety of management events using the ManagementEventWatcher class. The accessible data can be from both managed and unmanaged components in the distributed environment.
  2. Double click the ProcessorToolStripButton to create the Click Event Handler.
  3. Add the following code to the ProcessorToolStripButton Click Event Handler:
private void ProcessorToolStripButton_Click(object sender, EventArgs e)
{
    try
    {
        StringBuilder processorsBuilder = new StringBuilder();
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");

        foreach (ManagementObject managementObject in searcher.Get())
        {
            foreach (PropertyData property in managementObject.Properties)
            {
               if (property.Value == null)
                   continue;

               if (property.Name.ToLower() == "ProcessorId".ToLower())
                    processorsBuilder.AppendLine(property.Value.ToString());
            }
        }

        MessageBox.Show(processorsBuilder.ToString(), "Available Processors",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ex.GetType().ToString(),
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
  1. In the above code:
    • Create an instance of the StringBuilder and name it processorsBuilder. This object will be used to store available processors Ids.
    • Then we create an instance of the ManagementObjectSearcher class and name it searcher. This class retrieves a collection of management objects based on a specified query. This class is one of the more commonly used entry points to retrieving management information.
    • We then pass the following WMI Query “Select * From Win32_Processor” to query for every available processors installed on the system.
    • Then we iterate in the collection of management objects returned from the earlier WMI Query.
    • When the Get() method on the searcher object is invoked, the ManagementObjectSearcher executes the given query in the specified scope and returns a collection of management objects that match the query in a ManagementObjectCollection.
    • Then we iterate in the properties of each ManagementObject
    • If the ManagementObject Property is null we continue to the next property
    • If the property name of the ManagementObject equals ProcessorId then we append a line to the processorsBuilder with the property value that represents the processor Id.
    • Then we display the available processor Ids in a MessageBox as follows:

Available Processors

  1. Build and run the application and click the ProcessorToolStripButton this will display available Processors on your machine.

Thanks to Adavesh he posted a comment on this article and his code snippet below is more faster than the above code snippet.

private void ProcessorToolStripButton_Click(object sender, EventArgs e)
{
    string processorId = string.Empty;
    ManagementClass processorManagement = new ManagementClass("Win32_Processor");

    foreach (var processor in processorManagement.GetInstances())
    {
        try
        {
            processorId = processor["ProcessorId"].ToString();
            MessageBox.Show(processorId,"Processor Id",

                MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message,"An error occured in getting processor Id",
                MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
    }
}   

Now you have an application that application that retrieves your machine Processor Id.

Similar Posts