在 C# 中获取唯一的系统标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1308956/
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
Get Unique System Identifiers in C#
提问by Alex
what kind of 'unique' system identifiers can be easily obtained using C# (to hash and then uniquely identify a system)? I could just hash HDD size and things like that but I need to identify and distinguish computers that are all built by the same components so I can't go by that.
使用 C# 可以轻松获得什么样的“唯一”系统标识符(散列然后唯一标识系统)?我可以只是散列硬盘大小和类似的东西,但我需要识别和区分所有由相同组件构建的计算机,所以我不能这样做。
Appreciate hints, ideas, sample code!
欣赏提示、想法、示例代码!
采纳答案by JP Alioto
Here's a good start with WMI...
这是WMI的良好开端...
// Win32_CPU will work too
var search = new ManagementObjectSearcher( "SELECT * FROM Win32_baseboard" );
var mobos = search.Get();
foreach (var m in mobos)
{
var serial = m["SerialNumber"]; // ProcessorID if you use Win32_CPU
}
You can do that with many pieces of hardware and come up with a solution.
您可以使用许多硬件来做到这一点并提出解决方案。
回答by Reed Copsey
You can use WMIwith C# to get quite a bit of information about hardware.
您可以将WMI与 C# 结合使用以获取有关硬件的大量信息。
Hard Disk IDs and Ethernet MAC Address are two common unique identifiers used in system identification schemes. The MAC Address is, in theory, unique per network card.
硬盘 ID 和以太网 MAC 地址是系统识别方案中使用的两种常见唯一标识符。理论上,MAC 地址是每个网卡唯一的。
回答by Vinko Vrsalovic
You can use the System.Managementnamespace to get all stuff related to the hardware, ProcessorId, MAC addresses, and a lot more info you can then hash together.
您可以使用System.Management命名空间来获取与硬件、ProcessorId、MAC 地址以及更多信息相关的所有内容,然后您可以将它们散列在一起。
Take a look at this article:
看看这篇文章:
http://www.codeproject.com/KB/system/GetHardwareInformation.aspx
http://www.codeproject.com/KB/system/GetHardwareInformation.aspx
回答by Dan Diplo
Presuming you are talking about Windows, then each Windows installation has a unique product id (which you can see when you view the properties of My Computer). I think this is stored in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion:ProductId(REG_SZ)
. I take it you want more than that?
假设您在谈论 Windows,那么每个 Windows 安装都有一个唯一的产品 ID(您可以在查看我的电脑的属性时看到)。我认为这是存储在HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion:ProductId(REG_SZ)
. 我想你想要的不止这些?
回答by SwDevMan81
You could look into using GUID's
您可以考虑使用GUID
What is a GUID
什么是 GUID
For those of you who don't know, a GUID (pronounced goo'id - Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to create a autoincrementing integer, another way would be to create a GUID for your products.
对于那些不知道的人,GUID(读作 goo'id - 全球唯一标识符)是一个 128 位整数,可用于唯一标识某些内容。您可能将用户或产品存储在数据库中,并且希望以某种方式唯一标识数据库中的每一行。一种常见的方法是创建一个自动递增的整数,另一种方法是为您的产品创建一个 GUID。
How to create a GUID in C#
如何在 C# 中创建 GUID
The GUID method can be found in the System namespace. The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class.
GUID 方法可以在 System 命名空间中找到。GUID 方法 System.Guid.NewGuid() 初始化 GUID 类的新实例。
There are also a few overloads available for those of you who want the GUID formatted in a particular fashion.
对于希望以特定方式格式化 GUID 的人,还有一些重载可用。
The following live sample will output the GUID generated, the source code is also below.
下面的实时示例将输出生成的 GUID,源代码也在下面。
Response.Write(@"<br>System.Guid.NewGuid().ToString() = " + System.Guid.NewGuid().ToString());
Response.Write(@"<br>System.Guid.NewGuid().ToString(""N"") = " + System.Guid.NewGuid().ToString("N"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""D"") = " + System.Guid.NewGuid().ToString("D"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""B"") = " + System.Guid.NewGuid().ToString("B"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""P"") = " + System.Guid.NewGuid().ToString("P"));
回答by LukeH
Network card MAC address:
网卡MAC地址:
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
// probably need to do some filtering on ni.NetworkInterfaceType here
Console.WriteLine(ni.GetPhysicalAddress());
}