简体   繁体   中英

C# sometimes ID of CPU gets string of Zeros and also Motherboard ID is empty string

I have a software installed on my clients' PC, I give the license key to clients according to their CPU ID and Motherboard ID. This code is working correctly on most PCs but sometimes return incorrect value for CPU or MB, for example:

CPU ID = “0000000000000000”

MB = “” // empty string

 string results = string.Empty; string cpuID = string.Empty; ManagementClass mc = new ManagementClass("win32_processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuID = mo.Properties["processorID"].Value.ToString(); } results = cpuID; //get motherboard ID: string mbID = string.Empty; string query = "SELECT * FROM Win32_BaseBoard"; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); foreach (ManagementObject info in searcher.Get()) { mbID = info.GetPropertyValue("SerialNumber").ToString(); } results += "\r\n" + mbID; return results;

I'm in a hard situation because this issue is rare, I made about 100 licenses and this case happened 5 times only, it occurred on a client PC not on my PC, so trying a new code is really so hard. Any idea please? Or someone knows what is the possible reason. Thanks

It can be many components and with the code you have, you get at best the last value since you are not storing with string.concat() or in a list of strings. So my guess is that you are getting the last element of a list of components which has empty values, or you get an error when you are trying to retrieve a single value from a list of values. For example if the computer has more than one processor it might be that

mo.Properties["processorID"].Value.ToString()

goes into error because you have more than one value, so your code skips the entire block and the two strings 'results' and 'cpuID' will stay empty. It's only speculations but my suggestion would be to try debug your code in a computer with multiple processors and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM