提供用戶硬件信息輔助類HardwareHelper

實現效果

  • 本輔助類主要是用來方便實現獲取用戶硬件的相關信息,根據用戶各種硬件(CPU、網卡、磁盤、主板等)標識信息,獲取一個8位唯一標識數字。
  • 本輔助類只提供了一個公共的方法Value,它集合了CPU、網卡、磁盤、主板等信息,通過這個8位的數字標識,我們可以唯一區分一臺電腦,類似我們每個人的指紋信息一樣,很少會發生重複的現象,可以用在一些關鍵的註冊授權機制上。

實現步驟

  • 在代碼引用相關的代碼實現動態調用
/// 根據用戶各種硬件(CPU、網卡、磁盤、主板等)標識信息,獲取一個8位唯一標識數字
public static string Value()
/// 獲取硬件的標識信息
private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
/// 獲取硬件的標識信息
private static string identifier(string wmiClass, string wmiProperty)
/// 獲取CPU信息
private static string cpuId()
/// 獲取BOIS主板標識信息
private static string biosId()
/// 獲取主硬盤的標識信息
private static string diskId()
/// 獲取主板的標識信息
private static string baseId()
/// 獲取主視頻控制器標識信息
private static string videoId()
/// 獲取第一個可用網卡信息
private static string macId()
/// 轉換內容爲8位數字
private static string pack(string text)
  • 輔助類HardwareHelper的使用例子代碼如下所示
string identity = HardwareHelper.Value();
MessageUtil.ShowTips(identity);

附上源碼

    /// <summary>
    /// 提供用戶硬件唯一信息的輔助類
    /// </summary>
    public class HardwareHelper
    {
        /// <summary>
        /// 根據用戶各種硬件(CPU、網卡、磁盤、主板等)標識信息,
        /// 獲取一個8位唯一標識數字
        /// </summary>
        public static string Value()
        {
            return pack(cpuId()
                    + biosId()
                    + diskId()
                    + baseId()
                    + videoId()
                    + macId());
        }

        /// <summary>
        /// 獲取硬件的標識信息
        /// </summary>
        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>
        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>
        /// 獲取CPU信息
        /// </summary>
        private static string cpuId()
        {
            //Uses first CPU identifier available in order of preference
            //Don't get all identifiers, as 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>
        /// 獲取BOIS主板標識信息
        /// </summary>
        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");
        }

        /// <summary>
        /// 獲取主板的標識信息
        /// </summary>
        /// <returns></returns>
        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>
        /// 獲取第一個可用網卡信息
        /// </summary>
        /// <returns></returns>
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
        }

        /// <summary>
        /// 轉換內容爲8位數字
        /// </summary>
        private static string pack(string text)
        {
            string retVal;
            int x = 0;
            int y = 0;
            foreach (char n in text)
            {
                y++;
                x += (n*y);
            }
            retVal = x.ToString() + "00000000";

            return retVal.Substring(0, 8);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章