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.

(238) Comments

Blog Message http://olivier.62770.free.fr/modules.php?name=Your_Account&op=userinfo&username=MatthewSet

Thank you ever so for you article post.Thanks Again. Really Great.


Blog Message https://minelution.com/forum/70830-the-right-place-to-locate-the-males-bracelets-online/0

Very nice info and right to the point. I am not sure if this is truly the best place to ask but do you guys have any ideea where to hire some professional writers? Thank you


Blog Message http://trippinflavors.com/biryani-by-kilo-best-dum-biryani/

Im obliged for the blog article.Really thank you! Fantastic.


Blog Message https://www.levo.com/vip-financing-solutions

Thank you for your article post.Really looking forward to read more. Cool.


Blog Message http://mypornbible.com

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


Blog Message http://boinc.riojascience.com/team_display.php?teamid=265258

Your style is so unique in comparison to other folks I have read stuff from. Many thanks for posting when you ave got the opportunity, Guess I all just book mark this site.


Blog Message http://femina.rol.ro/forum/discussion/282786/stay-with-this-page-right-now-and-hire-the-best-car-specialist-within-moments

Im obliged for the blog post.Much thanks again. Awesome.


Blog Message http://www.military-boot-reviews.com/original-swat-boots/original-swat-boots-durability-review/

Very good post.Really looking forward to read more. Great.


Blog Message http://dealnews.in

I really liked your article.Really thank you! Want more.


Blog Message http://used-furniture-kuwait.com

Thanks a lot for sharing this with all of us you actually know what you are talking about! Bookmarked. Kindly also visit my web site =). We could have a link exchange agreement between us!


Blog Message http://cooknxt.com/members/icepuffin6/activity/13180/

It as not that I want to duplicate your web-site, but I really like the style and design. Could you tell me which design are you using? Or was it custom made?


Blog Message http://www.roommatefinder.org/members/turkeytenor16/activity/615874/

Usually I donaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžt read this kind of stuff, but this was genuinely fascinating!


Blog Message http://xthekidsandteens.club/story.php?id=18275

Really enjoyed this article. Really Cool.


Blog Message https://itunes.apple.com/app/id1232637491

pretty practical stuff, overall I think this is worth a bookmark, thanks


Blog Message http://mycrickethighlights2.us

Say, you got a nice blog.Much thanks again. Keep writing.

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