C# 抓取 硬體序號

C# 抓取 硬體序號

C# 抓取 硬體序號

Code

        public static String GetDeviceCode()//C# 抓取 硬體序號
        {
            String StrResult = "";

            //擷取cpu序號  
            string cpuInfo = "";
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (mo.Properties["ProcessorId"].Value != null)
                    cpuInfo += mo.Properties["ProcessorId"].Value.ToString();
            }
            StrResult += cpuInfo;

            //擷取硬碟ID 
            string HDid = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                if (mo.Properties["Model"].Value != null)
                    HDid += (string)mo.Properties["Model"].Value.ToString();
            }
            StrResult += HDid;
            
            //擷取網卡硬體地址     
            string MACAddress = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true && mo["MacAddress"] != null)
                    MACAddress += mo["MacAddress"].ToString();
                mo.Dispose();
            }
            StrResult += MACAddress;
            StrResult = StrResult.Replace(" ", "");
            StrResult = StrResult.Replace(":", "");

            string hash;
            using (MD5 md5 = MD5.Create())//MD5 加密運算
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(StrResult);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    builder.Append(hashBytes[i].ToString("x2")); // 将每个字节转换为两位的十六进制字符串
                }

                hash = builder.ToString();
            }

            char[] charArray = hash.ToCharArray();//C#字串轉字元陣列
            Array.Reverse(charArray);//C#字元陣列反轉
            StrResult = hash + new string(charArray);//C#字元陣列轉字串[C#字串反轉]

            return StrResult.ToUpper(); //64個英文字
        }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *