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

 更新时间:2022年12月30日 09:00:19   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现获取机器码的功能,文中的示例代码讲解详细,对我们学习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;
}

到此这篇关于C#实现获取机器码的示例详解的文章就介绍到这了,更多相关C#获取机器码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#生成验证码图片的方法

    C#生成验证码图片的方法

    这篇文章主要为大家详细介绍了C#生成验证码图片的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • C# Redis学习系列(一)Redis下载安装使用

    C# Redis学习系列(一)Redis下载安装使用

    这篇文章主要为大家分享了C# Redis学习系列教程第一篇, Redis下载、安装、使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C#计算字符串哈希值(MD5、SHA)的方法小结

    C#计算字符串哈希值(MD5、SHA)的方法小结

    这篇文章主要介绍了C#计算字符串哈希值(MD5、SHA)的方法,以实例形式较为详细的分析总结了C#计算字符串哈希值的各种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • C#中基于流的XML文件操作笔记

    C#中基于流的XML文件操作笔记

    这篇文章主要介绍了C#中基于流的XML文件操作笔记,本文主要是讲解针对XmlReader和XmlWriters两个XML读写类的使用,需要的朋友可以参考下
    2015-06-06
  • 基于WPF简单实现Meesage消息提醒

    基于WPF简单实现Meesage消息提醒

    这篇文章主要介绍了如何利用WPF简单实现Meesage消息提醒,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2023-07-07
  • 一起详细聊聊C#中的Visitor模式

    一起详细聊聊C#中的Visitor模式

    Visitor模式表示一个作用于某对象结构中的各元素的操作,下面这篇文章主要给大家介绍了关于C#中Visitor模式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • C# 通过反射获取类型的字段值及给字段赋值的操作

    C# 通过反射获取类型的字段值及给字段赋值的操作

    这篇文章主要介绍了C# 通过反射获取类型的字段值及给字段赋值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C#中属性和成员变量的区别说明

    C#中属性和成员变量的区别说明

    本篇文章主要是对C#中属性和成员变量的区别进行了介绍说明。需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • 在winform中实现双向数据绑定的方法

    在winform中实现双向数据绑定的方法

    双向数据绑定是一种允许我们创建持久连接的技术,使模型数据和用户界面(UI)之间的交互能够自动同步,今天我想通过winform中DataGridView控件为例,介绍在winform中如何实现双向数据绑定,需要的朋友可以参考下
    2024-03-03
  • C#检查指定对象是否存在于ArrayList集合中的方法

    C#检查指定对象是否存在于ArrayList集合中的方法

    这篇文章主要介绍了C#检查指定对象是否存在于ArrayList集合中的方法,涉及C#中Contains方法的使用技巧,需要的朋友可以参考下
    2015-04-04

最新评论