首页 > 编程语言 >C#获取计算机唯一标识组装GUID ,延伸ManagementClass、WIN32_类库名

C#获取计算机唯一标识组装GUID ,延伸ManagementClass、WIN32_类库名

时间:2024-05-07 16:11:07浏览次数:14  
标签:类库 ManagementClass string C# computerGUID Win32 -- static identifier

using System.Management;
using System.Security.Cryptography;
using System.Text;
 
namespace SWin
{
    public class ComGUID
    {
        private static string computerGUID = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(computerGUID))
            {
 
 
                computerGUID = GetHash("CPU >> " + cpuId() + "\nBIOS >> " +
            biosId() + "\nBASE >> " + baseId() + videoId() + "\nMAC >> " + macId()
                                     );
                computerGUID = computerGUID.Substring(0, 4) + computerGUID.Substring(5, computerGUID.Length - 5);
                computerGUID = computerGUID.Substring(0, 24) + computerGUID.Substring(24, computerGUID.Length - 25).Replace("-", "");
            }
            return computerGUID;
        }
        private static string GetHash(string s)
        {
            MD5 sec = new MD5CryptoServiceProvider();
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] bt = enc.GetBytes(s);
            string str = GetHexString(sec.ComputeHash(bt));
            return str;
 
        }
        private static string GetHexString(byte[] bt)
        {
            string s = string.Empty;
            for (int i = 0; i < bt.Length; i++)
            {
                byte b = bt[i];
                int n, n1, n2;
                n = (int)b;
                n1 = n & 15;
                n2 = (n >> 4) & 15;
                if (n2 > 9)
                    s += ((char)(n2 - 10 + (int)'A')).ToString();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
            }
            return s;
        }
 
        #region Original Device ID Getting Code
        private static string identifier
        (string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            ManagementClass mc = new  ManagementClass(wmiClass);
            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;
        }
        //Return a hardware identifier
        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
          ManagementClass mc =  new ManagementClass(wmiClass);
            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>
        ///    //获取CPU序列号
        /// </summary>
        /// <returns></returns>
        private static string cpuId()
        {
            string retVal = identifier("Win32_Processor", "UniqueId");
            if (retVal == "")
            {
                retVal = identifier("Win32_Processor", "ProcessorId");
                if (retVal == "")
                {
                    retVal = identifier("Win32_Processor", "Name");
                    if (retVal == "")
                    {
                        retVal = identifier("Win32_Processor", "Manufacturer");
                    }
 
                    retVal += identifier("Win32_Processor", "MaxClockSpeed");
                }
            }
            return retVal;
        }
 
 
        /// <summary>
        /// //BIOS序列号
        /// </summary>
        /// <returns></returns>
        private 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>
        /// 获取驱动信息
        /// </summary>
        /// <returns></returns>
        private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")
            + identifier("Win32_DiskDrive", "Manufacturer")
            + identifier("Win32_DiskDrive", "Signature")
            + identifier("Win32_DiskDrive", "TotalHeads");
        }
 
 
         // 主板序列号
        private static string baseId()
        {
            return identifier("Win32_BaseBoard", "Model")
            + identifier("Win32_BaseBoard", "Manufacturer")
            + identifier("Win32_BaseBoard", "Name")
            + identifier("Win32_BaseBoard", "SerialNumber");
        }
 
 
        /// <summary>
        /// 显卡信息
        /// </summary>
        /// <returns></returns>
        private static string videoId()
        {
            return identifier("Win32_VideoController", "DriverVersion")
            + identifier("Win32_VideoController", "Name");
        }
 
 
        /// <summary>
        /// MAC地址
        /// </summary>
        /// <returns></returns>
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration",
                "MACAddress", "IPEnabled");
        }
        #endregion
    }
}

页面调用

this.txtCode.Text = ComGUID.Value();

ManagementClass 类

表示一个通用信息模型 (CIM) 管理类,管理类是 WMI 类。此类的成员可以访问 WMI 数据,使用一个特定的 WMI 类路径。

常使用的WIN32_类库名

Win32_DiskDrive--硬盘驱动器

Win32_MemoryDevice--内存设备

 Win32_PortConnector--端口连接器

 Win32_BaseBoard--主板

Win32_VideoController -显卡

Win32_Processor--(CPU)处理器

Win32_SystemBIOS--系统BIOS

Win32_NetworkAdapter--网络适配器

Win32_NetworkAdapterConfiguration--网络适配器配置

Win32_ComputerSystem--计算机系统

Win32_OperatingSystem--操作系统

Win32_Process--进程

Win32_Account--帐户
Win32_Group--组

标签:类库,ManagementClass,string,C#,computerGUID,Win32,--,static,identifier
From: https://www.cnblogs.com/guangzhiruijie/p/18177595

相关文章

  • TheadLocal类学习
    ThreadLocal是Java中一个非常实用的线程相关的类,它提供线程本地变量,即每个线程都有自己独立的变量副本,从而避免了线程安全问题。下面我将通过几个方面来帮助你理解并学习如何使用ThreadLocal。基本概念线程局部变量:每个线程都拥有一份 ThreadLocal 变量的副本,彼此之间互......
  • Unity性能优化——合批(Batching)的限制与失败原因汇总
    Unity中Batching大致可以分为StaticBatching,DynamicBatching,SRPBatching与GPUInstancing四大类,但在使用时我们经常会遇到合批失败的情况,这里汇总了四大类的合批使用限制与合批失败的关键错误信息.StaticBatching的限制额外的内存开销64000个顶点限制影响......
  • [转]openEuler 22.03 (LTS-SP1)安装最新版Docker(踩坑及解决方案)
    原文地址:openEuler22.03(LTS-SP1)安装最新版Docker(踩坑及解决方案)_openeulerdocker-CSDN博客openEuler22.03LTS-SP1要是直接yuminstalldocker,默认安装docker是18.09.0,这个版本Docker有个bug,所以还是安装个最新版Docker。1、先增加docker官方仓库[[email protected]......
  • oracle表导出mysql适用的脚本方法
    oracle表导出mysql适用的脚本方法1.在对应的oracle数据库plsql中执行下面代码,建F_LIMS_GET_SQL_FOR_MYSQL函数CREATEORREPLACEFUNCTIONF_LIMS_GET_SQL_FOR_MYSQL(PI_TABLENAMEINVARCHAR2,PI_ISDROPININTEGER:=1......
  • DNS/DHCP 服务器
    DNS/DHCP服务器(Dnsmasq)(01)安装DnsmasqDnsmasq:安装  安装Dnsmasq,它是轻量级的DNS转发器和DHCP服务器软件。[1] 安装Dnsmasq。root@dlp:~#apt-yinstalldnsmasq [2] 配置Dnsmasq。root@dlp:~#vi/etc/dnsmasq.conf#line19:uncomment#neverforwar......
  • [Unit testing - React] Use the waitForElementToBeRemoved Async Util to Await Unt
    Sometimes,youmightneedtowaitforanelementtodisappearfromyourUIbeforeproceedingwithyourtestsetupormakingyourassertion.Inthislesson,wewilllearnaboutawrapperaroundthewaitForthatallowsyoutowaituntilanelementisremove......
  • docker网络配置:bridge模式、host模式、container模式、none模式
    在docker平台里有四种网络模式,今天继续分享一下它们的常用知识,进一步加深对docker技术的理解。1、docker网络模式分类dockerrun创建Docker容器时,可以用--net选项指定容器的网络模式,Docker主要有以下4种网络模式。bridge模式:--net=bridge如果不指定的话默认设置。host模式......
  • C#中面向对象的一些基础概念
    案例所创建的.cs如下:OOP--ObjectOrientedProgramming实例化类baseclassbc=newbaseclass();subclasssc=newsubclass();bc.Func();sc.Func();里氏转换子类对象可以直接赋值给父类变量子类可以调用父类对象,但是父类只有调用自己父类中如果是子类对象,则......
  • [Unit Testing - React] Use the Testing Playground to Help Decide Which Query to
    Queryingisdifficult!Evenifwefollowtheguidingprinciplesandalwaysstartfromthequeriesaccessibletoeveryone,weareboundtogetstucksometimes.Tohelpusout,theTestingPlaygroundwascreated.Inthislesson,wearegoingtoseehowtou......
  • python-magic:检测文件的MIME类型
    简介python-magic是一个Python封装的文件类型识别库,它基于libmagic库。libmagic是一个强大的文件类型识别工具,它可以分析文件内容来确定文件的MIME类型。通过python-magic,我们可以在Python脚本中轻松地调用这个功能,无论是用于文件处理、上传下载的文件类型检查,还是在自动化脚本中......