在 C# 中获取唯一标识符的最佳方法 [英] Best way to get a unique identifier in C#

查看:146
本文介绍了在 C# 中获取唯一标识符的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过一个类似的问题,但我现在正在减少对我需要的一些限制.

I've asked a similar question before, but I'm now reducing some of the restrictions about what I need.

我需要使用 C# 高效地在计算机上找到唯一标识符.它应该在任何特定计算机上始终保持不变,并且可以是多种因素的组合 - 只要它易于检索.

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.

我考虑过在 WMI 网络查询中使用 MAC 地址,但这太慢了,因为通常有 10 个以上的适配器.使用 WHERE IPEnabled=true 子句可能会更好,但我认为可能有比这更好的方法.

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.

想法?

(P.S.它不必完全独特.只要碰撞的机会很小,它就是完美的.)

(P.S. It doesn't have to be TOTALLY unique. As long as the chance of collision is small, it's perfect.)

推荐答案

First Get the Processor ID like this: (Add Reference to System.Management)

First Get the Processor ID like following: (Add Reference to 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"];
    }

你可以把上面两个连接起来,得到一个唯一的ID

You can concat the above two and get a unique ID

    string myUniqueID = id + motherBoard;
    Console.WriteLine(myUniqueID);

另请查看此链接 查找计算机的硬件 ID、CPU ID、主板 ID、硬盘 ID

这篇关于在 C# 中获取唯一标识符的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆