C#读取设备信息(源代码下载)
(一).说明
取得设备网卡的MAC地址和硬盘的卷标号
(二).图片示例
(三).代码
using System;
using System.Management;
namespace NetCard
{
/// <summary>
/// 读取设备
/// </summary>
public class run
{
private ManagementClass mc;
private ManagementObjectCollection moc;
private ManagementObject disk;
public run()
{}
/// <summary>
/// 取得设备网卡的MAC地址
/// </summary>
public string GetNetCardMacAddress()
{
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
str = mo["MacAddress"].ToString();
}
return str;
}
/// <summary>
/// 取得设备硬盘的卷标号
/// </summary>
/// <returns></returns>
public string GetDiskVolumeSerialNumber()
{
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
}
}
using System.Management;
namespace NetCard
{
/// <summary>
/// 读取设备
/// </summary>
public class run
{
private ManagementClass mc;
private ManagementObjectCollection moc;
private ManagementObject disk;
public run()
{}
/// <summary>
/// 取得设备网卡的MAC地址
/// </summary>
public string GetNetCardMacAddress()
{
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
str = mo["MacAddress"].ToString();
}
return str;
}
/// <summary>
/// 取得设备硬盘的卷标号
/// </summary>
/// <returns></returns>
public string GetDiskVolumeSerialNumber()
{
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
}
}
(四).示例下载
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。