简体   繁体   English

C# 有时 CPU 的 ID 得到零字符串,并且主板 ID 是空字符串

[英]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.我在客户的 PC 上安装了一个软件,我根据客户的 CPU ID 和主板 ID 将许可证密钥提供给客户。 This code is working correctly on most PCs but sometimes return incorrect value for CPU or MB, for example:此代码在大多数 PC 上都能正常工作,但有时会返回错误的 CPU 或 MB 值,例如:

CPU ID = “0000000000000000” CPU ID =“0000000000000000”

MB = “” // empty string MB = “” // 空字符串

 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.我处境艰难,因为这个问题很少见,我制作了大约 100 个许可证,而这种情况只发生了 5 次,它发生在客户端 PC 上而不是我的 PC 上,所以尝试新代码真的很难。 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.它可以是许多组件,并且使用您拥有的代码,您最多可以获得最后一个值,因为您没有使用string.concat()或字符串列表进行存储。 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.因为您有多个值而出错,因此您的代码会跳过整个块,并且两个字符串 'results' 和 'cpuID' 将保持为空。 It's only speculations but my suggestion would be to try debug your code in a computer with multiple processors and see what happens.这只是猜测,但我的建议是尝试在具有多个处理器的计算机中调试您的代码,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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