C#读取计算机的机器码、网卡、设备码
生活随笔
收集整理的這篇文章主要介紹了
C#读取计算机的机器码、网卡、设备码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/// <summary>
/// 取得設備硬盤的卷標號
/// </summary>
/// <returns></returns>
public static string GetDiskVolumeSerialNumber(){ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");disk.Get();return disk.GetPropertyValue("VolumeSerialNumber").ToString();}/// <summary>
/// 獲得CPU的序列號
/// </summary>
/// <returns></returns>
public static string getCpu(){string strCpu = null;ManagementClass myCpu = new ManagementClass("win32_Processor");ManagementObjectCollection myCpuConnection = myCpu.GetInstances();foreach (ManagementObject myObject in myCpuConnection){strCpu = myObject.Properties["Processorid"].Value.ToString();break;}return strCpu;
}
然后再組合生成機器碼:
/// <summary>/// 生成機器碼/// </summary>/// <returns></returns>public static string getMNum(){string strNum = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬盤序列號string strMNum = strNum.Substring(0, 24);//從生成的字符串中取出前24個字符做為機器碼return strMNum;}public int[] intCode = new int[127];//存儲密鑰public int[] intNumber = new int[25];//存機器碼的Ascii值public char[] Charcode = new char[25];//存儲機器碼字public void setIntCode()//給數組賦值小于10的數{for (int i = 1; i < intCode.Length; i++){intCode[i] = i % 9;}}生成注冊碼等:
/// <summary>/// 生成注冊碼/// </summary>/// <returns></returns>public string getRNum(){setIntCode();//初始化127位數組string MNum = getMNum();//獲取注冊碼for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中{Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1));}for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一個整數組中。{intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);}string strAsciiName = "";//用于存儲注冊碼for (int j = 1; j < intNumber.Length; j++){if (intNumber[j] >= 48 && intNumber[j] <= 57)//判斷字符ASCII值是否0-9之間{strAsciiName += Convert.ToChar(intNumber[j]).ToString();}else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判斷字符ASCII值是否A-Z之間{strAsciiName += Convert.ToChar(intNumber[j]).ToString();}else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判斷字符ASCII值是否a-z之間{strAsciiName += Convert.ToChar(intNumber[j]).ToString();}else//判斷字符ASCII值不在以上范圍內{if (intNumber[j] > 122)//判斷字符ASCII值是否大于z{strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();}else{strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();}}}return strAsciiName;//返回注冊碼}public static string GetNetCardMACAddress(){try{ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");string NetCardMACAddress = "";foreach (ManagementObject mo in searcher.Get()){NetCardMACAddress = mo["MACAddress"].ToString().Trim();}return NetCardMACAddress;}catch{return "";}}?
總結
以上是生活随笔為你收集整理的C#读取计算机的机器码、网卡、设备码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript/html brea
- 下一篇: OPC:客户端开发——应用WTclien