News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
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.

 

ProcessorId_01

 

6.      Import the System.Management namespace  using the following statement:

using System.Management;

7.      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.

8.      Double click the ProcessorToolStripButton to create the Click Event Handler.

9.      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);

    }

}

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);

        }

    }

}   

 

 

a.      Create an instance of the StringBuilder and name it processorsBuilder. This object will be used to store available processors Ids.

b.     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.

c.      We then pass the following WMI Query “Select * From Win32_Processor” to query for every available processors installed on the system.

d.     Then we iterate in the collection of management objects returned from the earlier WMI Query.

e.     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.

f.       Then we iterate in the properties of each ManagementObject

g.      If the ManagementObject Property is null we continue to the next property

h.     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.

i.        Then we display the available processor Ids in a MessageBox as follows:

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

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

(168) Comments

Blog Message http://www.dailymotion.com/video/x5vtzlw

Major thanks for the article post. Really Cool.


Blog Message https://www.floorswitch.com/contact.html

Really informative blog article.Much thanks again. Great.


Blog Message https://rideshareapps.com/uber-promo-codes-for-existing-users/

I simply could not leave your site prior to suggesting that I extremely enjoyed the standard information a person supply to your visitors? Is going to be back incessantly to inspect new posts


Blog Message https://www.ridester.com/uber-promo-code/

story. I was surprised you aren at more popular given that you definitely possess the gift.


Blog Message https://www.ridester.com/lyft-promo-codes-existing-users/

Wow! this is a great and helpful piece of info. I am glad that you shared this helpful info with us. Please stay us informed like this. Keep writing.


Blog Message https://www.hotfrog.com.au/business/wa/balcatta/oz-west-fencing-gates

Many thanks for sharing this good post. Very interesting ideas! (as always, btw)


Blog Message https://onlineshoppinginindiatrg.wordpress.com/2016/12/12/nikon-d3400-price-in-india-nikon-india-nikon-digital-camera-dealer/

Looking forward to reading more. Great blog.Really looking forward to read more. Keep writing.


Blog Message http://www.tmes.tc.edu.tw/~tmeslunch/modules/profile/userinfo.php?uid=635702

Wow, great blog post.Thanks Again. Will read on


Blog Message https://www.fiverr.com/mahmudulhossain

Thanks so much for the blog article.


Blog Message http://www.suba.me/

2HBzeN Im obliged for the blog post. Fantastic.


Blog Message https://www.amazon.com/BZONE-Ultimate-Nootropic-Performance-Supplement/dp/B06Y6G5TNX

Im grateful for the blog.Really looking forward to read more. Keep writing.


Blog Message http://www.seoinvancouver.com/

Some genuinely great posts on this site, thankyou for contribution.


Blog Message https://www.inspirationalclothingandaccessories.com/

Useful item would it live Satisfactory if i change interested in Greek in support of my sites subscribers? Thanks


Blog Message http://closestdispensaries.com/

Loving the info on this site, you have done outstanding job on the blog posts.


Blog Message http://closestdispensaries.com/

It as really a nice and helpful piece of info. I am glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.

First Previous 1 2 3 4 5 6 7 8 9 10  ... Next Last 
Rating
Name
Email
Comment