FCL小应用系列------------如何获取网卡等硬件信息

要想获取运行主机的各种配置信息,如网卡MAC地址,IP地址,硬盘分区,硬盘号等等,只要引用System.Management就可以轻松实现这一切。比起VC++的实现简单的多得多。例如获取当前使能的网卡MAC,并显示在列表框控件中:

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) listBox1.Items.Add ( mo["MacAddress"].ToString()); }

上述代码,只能获取到激活的网卡,并且网卡必须有连线。要想在网络连线断开的情况下也能获取到MAC,则需要如下代码

 

 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["MacAddress"] != null) listBox1.Items.Add ( mo["MacAddress"].ToString()); }

上述代码,可以获取到断线的网卡MAC,但是不能获取到禁用的网卡MAC。

 

另外,获取CPU序列号,硬盘序列号的代码如下:

private void GetInfo() { string cpuInfo = "";//cpu序列号 ManagementClass cimobject = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach(ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); Console.WriteLine ("cpu序列号:"+cpuInfo.ToString ()); } //获取硬盘ID String HDid; ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc1 = cimobject1.GetInstances(); foreach(ManagementObject mo in moc1) { HDid = (string)mo.Properties["Model"].Value; Console.WriteLine("硬盘序列号:"+HDid.ToString ()); }

 

你可能感兴趣的:(FCL小应用系列------------如何获取网卡等硬件信息)