場景 首先獲取設備硬碟的捲標號,然後獲取CPU的序列號,將這兩個拼接成機器碼。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建一個Winform程式,然後添加Mana ...
場景
首先獲取設備硬碟的捲標號,然後獲取CPU的序列號,將這兩個拼接成機器碼。
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
新建一個Winform程式,然後添加Management引用
然後進入Form1頁面的代碼中
首先獲取設備硬碟的捲標號代碼
public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); }
然後是獲取CPU的序列號的代碼
public 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; }
完整頁面示例代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.Management; namespace GetMCodeByCPUAndYP { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { label1.Text = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬碟序列號 } //取得設備硬碟的捲標號 public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } //獲得CPU的序列號 public 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; } } }