文档章节

【转载】使用C#获取CPU及硬盘序列号的源代码

编程码农
 编程码农
发布于 2016/05/14 16:02
字数 610
阅读 40
收藏 0

首先需添加对System.Management的引用。 

using System; 
using System.Runtime.InteropServices; 
using System.Management; 

namespace Hardware 
{ 
    /// <summary> 
    /// HardwareInfo 的摘要说明。 
    /// </summary> 
    public class HardwareInfo 
    { 
        //取机器名  
        public string GetHostName() 
        { 
            return System.Net.Dns.GetHostName(); 
        } 
        //取CPU编号 
        public String GetCpuID() 
        { 
            try 
            { 
                ManagementClass mc = new ManagementClass("Win32_Processor"); 
                ManagementObjectCollection moc = mc.GetInstances(); 

                String strCpuID = null; 
                foreach (ManagementObject mo in moc) 
                { 
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString(); 
                    break; 
                } 
                return strCpuID; 
            } 
            catch 
            { 
                return ""; 
            } 

        }//end method 

        //取第一块硬盘编号 
        public String GetHardDiskID() 
        { 
            try 
            { 
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); 
                String strHardDiskID = null; 
                foreach (ManagementObject mo in searcher.Get()) 
                { 
                    strHardDiskID = mo["SerialNumber"].ToString().Trim(); 
                    break; 
                } 
                return strHardDiskID; 
            } 
            catch 
            { 
                return ""; 
            } 
        }//end  

        public enum NCBCONST 
        { 
            NCBNAMSZ = 16,      /* absolute length of a net name         */ 
            MAX_LANA = 254,      /* lana's in range 0 to MAX_LANA inclusive   */ 
            NCBENUM = 0x37,      /* NCB ENUMERATE LANA NUMBERS            */ 
            NRC_GOODRET = 0x00,      /* good return                              */ 
            NCBRESET = 0x32,      /* NCB RESET                        */ 
            NCBASTAT = 0x33,      /* NCB ADAPTER STATUS                  */ 
            NUM_NAMEBUF = 30,      /* Number of NAME's BUFFER               */ 
        } 

        [StructLayout(LayoutKind.Sequential)] 
        public struct ADAPTER_STATUS 
        { 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] 
            public byte[] adapter_address; 
            public byte rev_major; 
            public byte reserved0; 
            public byte adapter_type; 
            public byte rev_minor; 
            public ushort duration; 
            public ushort frmr_recv; 
            public ushort frmr_xmit; 
            public ushort iframe_recv_err; 
            public ushort xmit_aborts; 
            public uint xmit_success; 
            public uint recv_success; 
            public ushort iframe_xmit_err; 
            public ushort recv_buff_unavail; 
            public ushort t1_timeouts; 
            public ushort ti_timeouts; 
            public uint reserved1; 
            public ushort free_ncbs; 
            public ushort max_cfg_ncbs; 
            public ushort max_ncbs; 
            public ushort xmit_buf_unavail; 
            public ushort max_dgram_size; 
            public ushort pending_sess; 
            public ushort max_cfg_sess; 
            public ushort max_sess; 
            public ushort max_sess_pkt_size; 
            public ushort name_count; 
        } 

        [StructLayout(LayoutKind.Sequential)] 
        public struct NAME_BUFFER 
        { 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] name; 
            public byte name_num; 
            public byte name_flags; 
        } 

        [StructLayout(LayoutKind.Sequential)] 
        public struct NCB 
        { 
            public byte ncb_command; 
            public byte ncb_retcode; 
            public byte ncb_lsn; 
            public byte ncb_num; 
            public IntPtr ncb_buffer; 
            public ushort ncb_length; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] ncb_callname; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] ncb_name; 
            public byte ncb_rto; 
            public byte ncb_sto; 
            public IntPtr ncb_post; 
            public byte ncb_lana_num; 
            public byte ncb_cmd_cplt; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] 
            public byte[] ncb_reserve; 
            public IntPtr ncb_event; 
        } 

        [StructLayout(LayoutKind.Sequential)] 
        public struct LANA_ENUM 
        { 
            public byte length; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)] 
            public byte[] lana; 
        } 

        [StructLayout(LayoutKind.Auto)] 
        public struct ASTAT 
        { 
            public ADAPTER_STATUS adapt; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)] 
            public NAME_BUFFER[] NameBuff; 
        } 
        public class Win32API 
        { 
            [DllImport("NETAPI32.DLL")] 
            public static extern char Netbios(ref NCB ncb); 
        } 

        public string GetMacAddress() 
        { 
            string addr = ""; 
            try 
            { 
                int cb; 
                ASTAT adapter; 
                NCB Ncb = new NCB(); 
                char uRetCode; 
                LANA_ENUM lenum; 

                Ncb.ncb_command = (byte)NCBCONST.NCBENUM; 
                cb = Marshal.SizeOf(typeof(LANA_ENUM)); 
                Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); 
                Ncb.ncb_length = (ushort)cb; 
                uRetCode = Win32API.Netbios(ref Ncb); 
                lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM)); 
                Marshal.FreeHGlobal(Ncb.ncb_buffer); 
                if (uRetCode != (short)NCBCONST.NRC_GOODRET) 
                    return ""; 

                for (int i = 0; i < lenum.length; i++) 
                { 
                    Ncb.ncb_command = (byte)NCBCONST.NCBRESET; 
                    Ncb.ncb_lana_num = lenum.lana[i]; 
                    uRetCode = Win32API.Netbios(ref Ncb); 
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET) 
                        return ""; 

                    Ncb.ncb_command = (byte)NCBCONST.NCBASTAT; 
                    Ncb.ncb_lana_num = lenum.lana[i]; 
                    Ncb.ncb_callname[0] = (byte)'*'; 
                    cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF; 
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); 
                    Ncb.ncb_length = (ushort)cb; 
                    uRetCode = Win32API.Netbios(ref Ncb); 
                    adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS)); 
                    Marshal.FreeHGlobal(Ncb.ncb_buffer); 

                    if (uRetCode == (short)NCBCONST.NRC_GOODRET) 
                    { 
                        if (i > 0) 
                            addr += ":"; 
                        addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}", 
                         adapter.adapt.adapter_address[0], 
                         adapter.adapt.adapter_address[1], 
                         adapter.adapt.adapter_address[2], 
                         adapter.adapt.adapter_address[3], 
                         adapter.adapt.adapter_address[4], 
                         adapter.adapt.adapter_address[5]); 
                    } 
                } 
            } 
            catch 
            { } 
            return addr.Replace(' ', '0'); 
        } 
    } 
} 

使用方法举例:

//获取硬盘序列号 
Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo(); 
string hardDiskID = hardwareInfo.GetHardDiskID(); 
System.Console.WriteLine(hardDiskID); 

//获取CPU序列号 
string cpuID = hardwareInfo.GetCpuID(); 
System.Console.WriteLine(cpuID); 
//获取硬盘序列号
Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo();
string hardDiskID = hardwareInfo.GetHardDiskID();
System.Console.WriteLine(hardDiskID);
//获取CPU序列号
string cpuID = hardwareInfo.GetCpuID();
System.Console.WriteLine(cpuID);

 

本文转载自:http://blog.csdn.net/songkexin/article/details/4916602

共有 人打赏支持
编程码农
粉丝 1
博文 19
码字总数 13170
作品 0
杭州
后端工程师
私信 提问
C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)

在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串。 1、硬盘 在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~ 什么叫硬盘序列号?指的是...

@winter
2016/11/03
0
0
【华为】服务器SCOM插件(CPU、内存、硬盘)开发项目

一、项目背景: 基于SCOM开发插件工作,实现对华为服务器的部分管理功能。 二、功能需求: 需求标题 需求描述 支持获取服务器列表 包括机架服务器,刀片服务器,高密服务器 支持和eSight对接...

华为众包
2017/09/26
1
0
C#获取CPU序列号/网卡MAC地址/硬盘序列号

C#获取CPU序列号代码 windows 管理规范 (wmi) 是可伸缩的系统管理结构,它采用一个统一的、基于标准的、可扩展的面向对象接口。wmi 为您提供与系统管理信息和基础 wmi api 交互的标准方法。w...

老朱教授
2017/11/28
0
0
使用C#获取CPU及硬盘序列号的源代码

首先需添加对System.Management的引用。 view plaincopy to clipboardprint? using System; using System.Runtime.InteropServices; using System.Management; namespace Hardware { /// //......

zting科技
2017/01/10
0
0
C#获取硬盘序列号

using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32; namespace Wjb.ReadOrWriteIniAndReg { /// /// HardDiskVal 的摘要说明......

晨曦之光
2012/03/09
409
0

没有更多内容

加载失败,请刷新页面

加载更多

《傲慢与偏见》的读书笔记与读后感作文2400字

《傲慢与偏见》的读书笔记与读后感作文2400字: 作者:孙苑馨;笔者按:读书这个习惯是我爸妈养出来的。小时候父母赚的钱除了吃饭穿衣剩下的就是买书了,他们除了买各种新鲜出版的文艺派图书...

原创小博客
16分钟前
1
0
oozie job 的挂了监控报警或重启

oozie Coordinator 的job 和actioni状态很多,但好像不支持设置某状态如failed后30分钟后自动重新拉启,因他的条件只有几种:触发条件可以是一个时间频率、一个dataset实例是否可用,或者可能...

hblt-j
35分钟前
1
0
python精简笔记-[6]-[if,for,while]语句

1.if 条件判断 语法: if 判断条件: 执行语句……elif 判断条件:    执行语句……elif 判断条件:    执行语句……else: 执行语句…… 实例1: n = input("请输...

平头哥-Enjoystudy
38分钟前
0
0
RxJava 是如何实现线程切换的(上)

前言 通过前一篇的从观察者模式出发,聊聊RxJava,我们大致理解了RxJava的实现原理,在RxJava中可以非常方便的实现不同线程间的切换。subscribeOn 用于指定上游线程,observeOn 用于指定下游线...

亭子happy
今天
2
0
想晋级高级工程师只知道表面是不够的!Git内部原理介绍

本文由云+社区发表 作者:腾讯工蜂用户:王二卫 从不一样的视角了解git,以便更好的使用git 一、git & git 版本库认识 git 是一个内容寻址的文件系统,其核心部分是一个简单的键值对数据库(...

腾讯云加社区
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部