C#讀取CPU序列號、硬盤ID、網卡MAC地址,生成機器碼

話很少說,直接上代碼,類庫中的機器碼使用序列號、硬盤ID、網卡MAC地址組合取MD5生成。ide

using System;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace WayneShao.Common
{
    internal static class MachineCode
    {
        private static string _machineCodeString;
        public static string Value
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_machineCodeString))
                    _machineCodeString = GetMD5($"{CPUCode}_{HDId}_{MacAddress}");
                return _machineCodeString;
            }
        }

        private static string _cpuCode;
        public static string CPUCode => _cpuCode ?? (_cpuCode = GetCPUInfo());

        private static string _hdId;
        public static string HDId => _hdId ?? (_hdId = GetHDid());

        private static string _macAddress;
        public static string MacAddress => _macAddress ?? (_macAddress = GetMACAddress());

        ///   <summary> 
        ///   獲取cpu序列號     
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetCPUInfo()
        {
            using (var cimobject = new ManagementClass("Win32_Processor"))
            {
                var hdids = cimobject.GetInstances().Cast<ManagementObject>().Select(o => o.Properties["ProcessorId"].Value).Cast<string>().ToArray();
                return hdids.Any() ? hdids.First() : string.Empty;
            }
        }

        ///   <summary> 
        ///   獲取硬盤ID     
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetHDid()
        {
            using (var cimobject1 = new ManagementClass("Win32_DiskDrive"))
            {
                var hdids = cimobject1.GetInstances().Cast<ManagementObject>().Select(o => o.Properties["Model"].Value).Cast<string>().ToArray();
                return hdids.Any() ? hdids.First() : string.Empty;
            }
        }

        ///   <summary> 
        ///   獲取網卡硬件地址 
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetMACAddress()
        {
            using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                var macs = mc.GetInstances().Cast<ManagementObject>().Where(o => (bool)o["IPEnabled"]).Select(o => o["MacAddress"].ToString()).ToArray();
                return macs.Any() ? macs.First() : string.Empty;
            }
        }

        ///   <summary> 
        ///   獲取字符串的MD5值
        ///   </summary> 
        ///   <returns> string </returns> 
        public static string GetMD5(string source)
        {
            var result = Encoding.Default.GetBytes(source);
            var md5 = new MD5CryptoServiceProvider();
            var output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }

    }
}

另附MSDN中關於 WMI Class 的文檔供你們參考spa

https://msdn.microsoft.com/zh-cn/library/aa394173(VS.85).aspxcode

相關文章
相關標籤/搜索