C#實(shí)現(xiàn)獲取機(jī)器碼的示例詳解
更新時(shí)間:2022年12月30日 09:00:19 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)獲取機(jī)器碼的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果
代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { label1.Text = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬盤序列號 string[] strid = new string[24]; for (int i = 0; i < 24; i++)//把字符賦給數(shù)組 { strid[i] = label1.Text.Substring(i, 1); } label1.Text = ""; Random rdid = new Random(); for (int i = 0; i < 24; i++)//從數(shù)組隨機(jī)抽取24個(gè)字符組成新的字符生成機(jī)器碼 { label1.Text += strid[rdid.Next(0, 24)]; } } //取得設(shè)備硬盤的卷標(biāo)號 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; } }
partial class Form1 { /// <summary> /// 必需的設(shè)計(jì)器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計(jì)器生成的代碼 /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Location = new System.Drawing.Point(87, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(0, 16); this.label1.TabIndex = 6; // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label3.Location = new System.Drawing.Point(104, 65); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(0, 16); this.label3.TabIndex = 7; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(28, 32); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 8; this.label4.Text = "機(jī)器碼:"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(353, 75); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label1); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "使用硬盤序列號和CPU序列號生成機(jī)器碼"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; }
到此這篇關(guān)于C#實(shí)現(xiàn)獲取機(jī)器碼的示例詳解的文章就介紹到這了,更多相關(guān)C#獲取機(jī)器碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
這篇文章主要給大家介紹了關(guān)于C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08C#中Trim()、TrimStart()、TrimEnd()的用法介紹
這篇文章主要介紹了C#中Trim()、TrimStart()、TrimEnd()的用法,有需要的朋友可以參考一下2014-01-01winform異型不規(guī)則界面設(shè)計(jì)的實(shí)現(xiàn)方法
這篇文章主要介紹了winform異型不規(guī)則界面設(shè)計(jì)的實(shí)現(xiàn)方法,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08C# 獲取數(shù)據(jù)庫中所有表名、列名的示例代碼
這篇文章主要介紹了C# 獲取數(shù)據(jù)庫中所有表名、列名,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06