为什么使用System.Management和SystemInfo.DeviceuniqueIdentifier Unity不同?
我有区分计算机的特定问题。我想在我的Unity应用程序和WPF应用中获得相同的唯一ID值。在 为我的WPF侧拿了代码
private string GetDeviceUniqueIdentifier() {
string ret = string.Empty;
string concatStr = string.Empty;
try {
using ManagementObjectSearcher searcherBb = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
foreach (var obj in searcherBb.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using ManagementObjectSearcher searcherBios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
foreach (var obj in searcherBios.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using ManagementObjectSearcher searcherOs = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (var obj in searcherOs.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using var sha1 = SHA1.Create();
ret = string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(concatStr)).Select(b => b.ToString("x2")));
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
return ret;
}
,我有 SystemInfo.deviceuniqueIdentifier 我的团结方面。在我的计算机上,这些东西返回相同的值,但是当我在相当弱的PC上测试它时,它们返回了不同的值:
WPF侧重新启动 5F5EA7E20B798FC1C16A7C65747F58E50519CB76 和
Unity Side返回 C23BE19B361FCC9683C544358CEA150695AC74CA 。我使用 2021.2.14f1 unity的版本。
这可能是什么原因?还是其他方法可以在.NET框架和团结中具有相同的独特ID?
我发现ManagementObjectSearcher(“从Win32_Baseboard中进行选择 * * *”)
和ManagementObjectSearcher(“从Win32_bios中进行选择 * *)
return> return 不适用。这很奇怪。我还尝试使用 string.empty 而不是它们,但是ID仍然不同。
I have a specific problem with distinguishing computers. I want to get the same unique Id values in my Unity app and in my WPF app. In this post I took the code
private string GetDeviceUniqueIdentifier() {
string ret = string.Empty;
string concatStr = string.Empty;
try {
using ManagementObjectSearcher searcherBb = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
foreach (var obj in searcherBb.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using ManagementObjectSearcher searcherBios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
foreach (var obj in searcherBios.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using ManagementObjectSearcher searcherOs = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (var obj in searcherOs.Get()) {
concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
}
using var sha1 = SHA1.Create();
ret = string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(concatStr)).Select(b => b.ToString("x2")));
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
return ret;
}
for my WPF side and I have SystemInfo.deviceUniqueIdentifier for my Unity side. On my computer these things return the same values, but when I tested it on quite weak PC, they returned different values:
WPF side retured 5f5ea7e20b798fc1c16a7c65747f58e50519cb76 and
Unity side returned c23be19b361fcc9683c544358cea150695ac74ca. I use 2021.2.14f1 version of Unity.
What could be the reason of this? Or is there other ways to have the same unique Ids in .NET Framework and in Unity?
I found out that ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard")
and ManagementObjectSearcher("SELECT * FROM Win32_BIOS")
return Not Applicable. This is quite strange. I also tried to use string.Empty instead of them, but Ids are still different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了解决方案,但我不确定它是稳定的解决方案。正如我所说:
我几乎放弃了,但尝试了最后一件事:
我在每个语句结束时添加了
.trim()
,这解决了我的问题。现在,我的 unity id等于 .net Framework ID。希望这对某人有帮助。Ok, I found the solution, but I'm not sure, that it is stable solution. As I said:
And I almost gave up, but tried the last one thing:
I added
.Trim()
at the end of each statement and this solved my problem. Now my Unity Id is equal to .Net Framework Id. Hope this will help someone.