C#获取硬件信息
using System; using System.Runtime.InteropServices; using System.Management; //using System.Management.Instrumentation; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using System.Security.Cryptography; using System.IO; namespace 获取硬盘序列号 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // 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 Win32_PhysicalMedia ////取第一块硬盘编号 //public String GetHardDiskID() //{ // ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_logicaldisk"); // String strHardDiskID = null; // foreach (ManagementObject mo in searcher.Get()) // { // strHardDiskID = mo["SerialNumber"].ToString().Trim(); // //strHardDiskID =Convert .ToString ( mo["SerialNumber"]); // MessageBox.Show(strHardDiskID); // } // return strHardDiskID; //}//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'); } [DllImport("DiskID32.dll")] public static extern long DiskID32(ref byte DiskModel, ref byte DiskID); public static string GetDiskID() { byte[] DiskModel = new byte[31]; byte[] DiskID = new byte[31]; int i; //string Model = ""; string ID = ""; if (DiskID32(ref DiskModel[0], ref DiskID[0]) != 1) { for (i = 0; i < 31; i++) { if (Convert.ToChar(DiskID[i]) != Convert.ToChar(0)) { ID = ID + Convert.ToChar(DiskID[i]); } } ID = ID.Trim(); } else { Console.WriteLine("获取硬盘序列号出错"); } return ID; } private void button1_Click(object sender, EventArgs e) { //获得硬盘序列号 textBox1.Text = GetDiskID(); //获得MAC地址 //textBox1.Text = GetMacAddress(); //获得机器名 //textBox1.Text = GetHostName(); //获得CPUID //textBox1.Text = GetCpuID(); } //获得磁盘序列号 private void Form1_Load(object sender, EventArgs e) { //try //{ // string hardDiskID = null; // ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_logicaldisk"); // foreach (ManagementObject id in searcher.Get()) // { // hardDiskID = Convert.ToString(id.GetPropertyValue("VolumeSerialNumber")); // this.textBox1.Text += string.Format(" 磁盘序列号为{0} \n\t", hardDiskID); // //hardDiskID = Convert.ToString(id.GetPropertyValue("HardDiskSerialNumber")); // ////this.textBox1.Text += string.Format(" 磁盘序列号为{0} \n\t", hardDiskID); // //this.textBox1.Text = hardDiskID; // } //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} } } }
添加对System.Management类库的引用。然后添加下述代码,就可获得CPU、主板和硬盘等的编号。
1,获取CPU编号
System.Management.ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
MessageBox.Show(mo["processorid"].ToString());
}
2,获取主板ID
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject mo in searcher.Get())
{
MessageBox.Show( mo["SerialNumber"].ToString().Trim());
}
或者:
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mx in moc)
{
MessageBox.Show( mx.Properties["SerialNumber"].Value.ToString());
}
3,获取硬盘ID
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
MessageBox.Show(mo.Properties["Model"].Value.ToString());
}
4,获取网卡地址
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
MessageBox.Show(mo["MacAddress"].ToString());
mo.Dispose();
}
5,获取内存信息
System.Management.ManagementClass mcMemory = new System.Management.ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection mocMemory = mcMemory.GetInstances();
foreach (ManagementObject mo in mocMemory)
{
if (mo.Properties["TotalVisibleMemorySize"].Value != null)
{
MessageBox.Show(mo.Properties["TotalVisibleMemorySize"].Value.ToString());
}
}
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com