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:
using System.Management;
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);
}
}
}
Now you have an application that application that retrieves your machine Processor Id.
15 March 2022
17 February 2022
09 December 2019