zoukankan      html  css  js  c++  java
  • C#获取计算机唯一标识组装GUID ,延伸ManagementClass、WIN32_类库名

    我们因为一些特殊需求需要用一个特定标识来标示一台计算机,在一些硬件配置不变的前提下,不论何时这个标识值都不改变,原本想着用自带的GUID生产,在配置文件里记录第一次值,以后直接读取,但是考虑到程序重新安装了,这个GUID还是会发生变化,对通信设别就会出现误差,C#自带的GUID生成很显然是不合适的。下面推荐组码方式,代码也是网上找来的。后面延伸总结了一下。

    首先:添加引用

    代码如下:

    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() + "
    BIOS >> " +
                biosId() + "
    BASE >> " + baseId() + videoId() + "
    MAC >> " + 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--组

  • 相关阅读:
    字符串旋转词、句子逆序化、字符串移位、最小字典序字符串问题
    字符串匹配算法——BF、KMP、Sunday
    字符串问题简述与两个基本问题的Java实现——判断二叉树拓扑结构关系与变形词
    PowerDesigner使用笔记
    SpringMVC学习笔记八:文件上传下载(转)
    小程序实现原理解析
    Flink流处理之迭代案例
    关于“淘宝爆款”的数据抓取与数据分析
    基于内容的推荐 java实现
    qt坐标系统与布局的简单入门
  • 原文地址:https://www.cnblogs.com/Cynosure/p/6381894.html
Copyright © 2011-2022 走看看