Here's the corrected code:
using System;
using System.Management;
class Program
{
static void Main()
{
try
{
// Get Processor ID
string processorId = "";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
{
foreach (ManagementObject obj in searcher.Get())
{
processorId = obj["ProcessorId"]?.ToString();
}
}
// Get Motherboard Serial Number
string motherboardSerial = "";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
{
foreach (ManagementObject obj in searcher.Get())
{
motherboardSerial = obj["SerialNumber"]?.ToString();
}
}
// Combine IDs for Unique Identification
string uniqueID = processorId + motherboardSerial;
Console.WriteLine($"Unique ID: {uniqueID}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Notes:
- Make sure to run the application with administrative privileges, as WMI queries often require them.
- The above code assumes the
System.Management
namespace is included in your project. If it's not available, add a reference to theSystem.Management
assembly.