如何在c#和vb.net中获取客户端机器的MAC地址

时间:2010-01-07 08:00:15

标签: c# .net asp.net vb.net

如何在c#和vb.net中获取客户端机器的MAC地址

3 个答案:

答案 0 :(得分:12)

我不确定客户端计算机的含义,因为您只能获取应用程序执行的计算机的NIC的MAC地址。

为此,您可以使用ManagementClass

C#:

using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
    foreach(ManagementObject mo in mc.GetInstances())
    {
        Console.WriteLine(mo["MacAddress"].ToString());
    }
}

VB.NET:

Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
    For Each mo As ManagementObject In mc.GetInstances()
        Console.WriteLine(mo("MacAddress").ToString())
    Next
End Using

答案 1 :(得分:5)

所需的答案是

ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();

    foreach (ManagementObject objMO in objMOC)
    {
        if (!(bool)objMO["ipEnabled"])
            continue;

        Console.WriteLine((string)objMO["MACAddress"]);
    }

答案 2 :(得分:3)

这应该适用于vb - 我相信c#接近这个 导入以下命名空间。

Imports System.Management

声明以下对象变量。

Dim objMOS As ManagementObjectSearcher

Dim objMOC As Management.ManagementObjectCollection

Dim objMO As Management.ManagementObject

执行查询。

objMOS = New ManagementObjectSearcher("Select * From Win32_NetworkAdapter")

objMOC = objMOS.Get

Get MAC address from the query result.

For Each objMO In objMOC

MessageBox.Show(objMO("MACAddress"))

Next

处置对象变量。

objMOS.Dispose()

objMOS = Nothing

objMO.Dispose()

objMO = Nothing