在 C# 中获取唯一标识符的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10310328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get a unique identifier in C#
提问by Chris Watts
I've asked a similar question before, but I'm now reducing some of the restrictions about what I need.
我之前问过一个类似的问题,但我现在正在减少对我需要的一些限制。
I need to find a unique identifier on a computer, efficiently, with C#. It should always stay the same on any particular computer, and can be a composite of many factors - provided it's simple to retrieve.
我需要使用 C# 高效地在计算机上找到唯一标识符。它应该在任何特定计算机上始终保持不变,并且可以是许多因素的组合 - 只要它易于检索。
I've thought about using the MAC address with WMI Network queries, but this is too slow as usually there are 10+ adapters. Might be better with a WHERE IPEnabled=true clause, but I think there's probably something better than this.
我考虑过在 WMI 网络查询中使用 MAC 地址,但这太慢了,因为通常有 10 个以上的适配器。使用 WHERE IPEnabled=true 子句可能会更好,但我认为可能有比这更好的方法。
Ideas?
想法?
(P.S. It doesn't have to be TOTALLY unique. As long as the chance of collision is small, it's perfect.)
(PS 不一定要完全独一无二。只要碰撞的机会很小,就完美了。)
回答by stephbu
Machine SID is stable as well - various ways to get to it below...
机器 SID 也很稳定——下面有各种方法来获得它......
回答by Likurg
Realy nice example you will see here Generating-Unique-for-a-Computer
非常好的示例,您将在此处看到Generating-Unique-for-a-Computer
回答by Habib
First Get the Processor ID like following: (Add Reference to System.Management)
首先获取处理器 ID,如下所示:(添加对 的引用System.Management)
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorID"].ToString();
}
//Then you can get the motherboard serial number:
//然后就可以得到主板序列号了:
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string motherBoard = "";
foreach (ManagementObject mo in moc)
{
motherBoard = (string)mo["SerialNumber"];
}
You can concat the above two and get a unique ID
可以concat上面两个,得到一个唯一的ID
string myUniqueID = id + motherBoard;
Console.WriteLine(myUniqueID);
Also check out this link Finding Hardware ID, CPU ID, Motherboard ID, Hard-Disk ID of a computer
回答by Ivan Karajas
You could look at this technical bulletin on Windows Product Activation. Judging by your comment, you probably don't want to go to these lengths, but maybe it can provide you with some inspiration...
您可以查看有关 Windows 产品激活的技术公告。从您的评论来看,您可能不想走这么远,但也许它可以为您提供一些灵感......
WPA generates a hash based on the serial numbers of some of the following hardware items:
WPA 根据以下某些硬件项目的序列号生成哈希:
- Display Adapter
- SCSI Adapter
- IDE Adapter
- Network Adapter (MAC address)
- RAM amount range
- Processor type
- Processor serial number
- Hard drive
- Hard drive volume serial number
- CD–ROM / CD-RW / DVD-ROM
- 显示适配器
- SCSI 适配器
- IDE适配器
- 网络适配器(MAC 地址)
- RAM量范围
- 处理器类型
- 处理器序列号
- 硬盘
- 硬盘卷序列号
- CD-ROM / CD-RW / DVD-ROM

