当前位置: 移动技术网 > IT编程>开发语言>c# > Winform中实现根据CPU和硬盘获取机器码

Winform中实现根据CPU和硬盘获取机器码

2020年03月16日  | 移动技术网IT编程  | 我要评论

场景

首先获取设备硬盘的卷标号,然后获取cpu的序列号,将这两个拼接成机器码。

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个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;
        }
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网