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.

(141) Comments

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

TdmNCx These are in fact wonderful ideas in on the topic of blogging. You have touched some pleasant things here. Any way keep up wrinting.


Blog Message http://kurwapolska.info/story.php?id=5728

Really informative blog article.Really looking forward to read more. Great.


Blog Message http://accesshealthcare.info/story/3975

Great, thanks for sharing this post.Thanks Again. Much obliged.


Blog Message http://www.grandeurparksresidences.sg

Thanks for sharing, this is a fantastic article post.Really looking forward to read more.


Blog Message http://expressioncanada.ca/discussion_fr/index.php?action=profile;u=82574

I truly appreciate this blog post.Really looking forward to read more.


Blog Message http://www.the-santorini.sg

Major thanks for the blog.Much thanks again. Great.


Blog Message http://www.theglades-condo.sg

Say, you got a nice article.Much thanks again. Will read on...


Blog Message http://www.theglades-condo.sg

I truly appreciate this blog post.Really looking forward to read more. Keep writing.


Blog Message http://www.seasonofstyle.com

I truly appreciate this blog.Really thank you! Want more.


Blog Message http://farmerslabseeds.com

wonderful post, very informative. I wonder why the opposite specialists of this sector do not understand this. You should proceed your writing. I am sure, you ave a great readers a base already!


Blog Message http://www.theglades-condo.sg

Spot on with this write-up, I really assume this web site needs much more consideration. I all in all probability be again to learn rather more, thanks for that info.


Blog Message https://hypertoolz.com/products/shills-deep-cleansing-black-mask-blackhead-removal-facial-mask-50ml

I really liked your blog post.Much thanks again. Will read on...


Blog Message http://www.newsbon.com

Just Browsing While I was surfing today I noticed a excellent article about


Blog Message http://koolfragrance.com

This is a topic which is close to my heart Thank you! Exactly where are your contact details though?


Blog Message http://oakshores.pro/story.php?id=6065

Wow, great article post. Will read on...

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