C# 获取计算机唯一标识
2022/7/14 1:25:45
本文主要是介绍C# 获取计算机唯一标识,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C# 获取计算机唯一标识
原文链接
private static string _sFingerPrint { get; set; } /// <summary> /// 计算机唯一标识 /// </summary> public static string sFingerPrint { get { if (string.IsNullOrEmpty(_sFingerPrint)) { _sFingerPrint = GetHash("UUID >> " + UUID() + "\r\nCPU >> " + CpuId() + "\r\nBIOS >> " + BiosId() + "\r\nBASE >> " + BaseId() + "\r\nDisk >> " + DiskId() + "\r\nVideo >> " + VideoId() + "\r\nMAC >> " + MacId()); } return _sFingerPrint; } } /// <summary> /// 获取硬件标识符 /// </summary> /// <param name="wmiClass"></param> /// <param name="wmiProperty"></param> /// <param name="wmiMustBeTrue"></param> /// <returns></returns> private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { if (mo[wmiMustBeTrue].ToString() == "True") { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } } return result; } /// <summary> /// 获取硬件标识符 /// </summary> /// <param name="wmiClass"></param> /// <param name="wmiProperty"></param> /// <returns></returns> private static string identifier(string wmiClass, string wmiProperty) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } return result; } /// <summary> /// 获取UUID /// </summary> /// <returns></returns> public static string UUID() { string code = null; SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct"); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) { foreach (var item in searcher.Get()) { using (item) code = item["UUID"].ToString(); } } return code; } /// <summary> /// 获取CPUID /// </summary> /// <returns></returns> public static string CpuId() { //Uses first CPU identifier available in order of preference //Don't get all identifiers, as it is very time consuming string retVal = identifier("Win32_Processor", "UniqueId"); if (retVal == "") //If no UniqueID, use ProcessorID { retVal = identifier("Win32_Processor", "ProcessorId"); if (retVal == "") //If no ProcessorId, use Name { retVal = identifier("Win32_Processor", "Name"); if (retVal == "") //If no Name, use Manufacturer { retVal = identifier("Win32_Processor", "Manufacturer"); } //Add clock speed for extra security retVal += identifier("Win32_Processor", "MaxClockSpeed"); } } return retVal; } /// <summary> /// 获取BIOSID /// </summary> /// <returns></returns> public static string BiosId() { return identifier("Win32_BIOS", "Manufacturer") + identifier("Win32_BIOS", "SMBIOSBIOSVersion") + identifier("Win32_BIOS", "IdentificationCode") + identifier("Win32_BIOS", "SerialNumber") + identifier("Win32_BIOS", "ReleaseDate") + identifier("Win32_BIOS", "Version"); } /// <summary> /// 获取硬盘ID /// </summary> /// <returns></returns> public static string DiskId() { return identifier("Win32_DiskDrive", "Model") + identifier("Win32_DiskDrive", "Manufacturer") + identifier("Win32_DiskDrive", "Signature") + identifier("Win32_DiskDrive", "TotalHeads"); } /// <summary> /// 获取主板ID /// </summary> /// <returns></returns> public static string BaseId() { return identifier("Win32_BaseBoard", "Model") + identifier("Win32_BaseBoard", "Manufacturer") + identifier("Win32_BaseBoard", "Name") + identifier("Win32_BaseBoard", "SerialNumber"); } /// <summary> /// 获取主视频控制器ID /// </summary> /// <returns></returns> public static string VideoId() { return identifier("Win32_VideoController", "DriverVersion") + identifier("Win32_VideoController", "Name"); } /// <summary> /// 获取网卡ID /// </summary> /// <returns></returns> public static string MacId() { return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); }
这篇关于C# 获取计算机唯一标识的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#