C#实现获取机器码的示例详解

作者:芝麻粒儿 时间:2021-11-13 06:57:19 

实践过程

效果

C#实现获取机器码的示例详解

代码

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++)//把字符赋给数组
       {
           strid[i] = label1.Text.Substring(i, 1);
       }
       label1.Text = "";
       Random rdid = new Random();
       for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器码
       {
           label1.Text += strid[rdid.Next(0, 24)];
       }
   }
   //取得设备硬盘的卷标号
   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>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.IContainer components = null;

/// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
   protected override void Dispose(bool disposing)
   {
       if (disposing && (components != null))
       {
           components.Dispose();
       }
       base.Dispose(disposing);
   }

#region Windows 窗体设计器生成的代码

/// <summary>
   /// 设计器支持所需的方法 - 不要
   /// 使用代码编辑器修改此方法的内容。
   /// </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 = "机器码:";
       //
       // 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序列号生成机器码";
       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;
}

来源:https://blog.csdn.net/qq_27489007/article/details/128402531

标签:C#,机器码
0
投稿

猜你喜欢

  • C#如何调用MFC 窗口 DLL

    2022-04-14 09:57:51
  • C# TSC打印二维码和条形码的实现方法

    2021-10-06 14:39:53
  • JVM类加载机制原理及用法解析

    2021-08-17 00:22:29
  • Redis在springboot中的使用教程

    2021-10-02 18:10:46
  • Android ImageView实现图片裁剪和显示功能

    2022-01-31 10:35:22
  • Java深入了解数据结构中常见的排序算法

    2021-11-02 23:33:53
  • Android蓝牙服务查找附近设备分析探索

    2023-04-01 19:50:21
  • android自动生成dimens适配文件的图文教程详解(无需Java工具类)

    2023-07-17 12:12:30
  • RecylerView实现流布局StaggeredGridLayoutManager使用详解

    2023-04-30 20:51:38
  • Android 调用系统应用的方法总结

    2023-05-05 09:53:52
  • Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法

    2023-09-06 19:53:30
  • Android的HTTP类库Volley入门学习教程

    2022-06-08 11:56:51
  • JAVA字符串拼接常见方法汇总

    2021-11-13 03:11:31
  • Android MotionEvent中getX()和getRawX()的区别实例详解

    2023-01-24 22:06:42
  • ElasticSearch如何设置某个字段不分词浅析

    2023-01-29 07:22:30
  • c#数组详解

    2023-01-31 13:09:08
  • C#多线程中的异常处理操作示例

    2023-07-16 06:10:10
  • C#反射内存的处理分析

    2022-04-30 00:56:22
  • android自定义按钮示例(重写imagebutton控件实现图片按钮)

    2021-06-13 07:55:19
  • Java聊天室之使用Socket实现传递对象

    2023-02-07 15:28:52
  • asp之家 软件编程 m.aspxhome.com