简体   繁体   中英

How to get MAC address of client machine in c# and vb.net

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

I am not sure what you mean by client machine , because you can only get the MAC address of a NIC of the machine your application executes under.

For this you could use 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

the desired answer is

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"]);
    }

This should work in vb - i am sure c# is close to this
Import the following namespace.

Imports System.Management

Declare following object variables.

Dim objMOS As ManagementObjectSearcher

Dim objMOC As Management.ManagementObjectCollection

Dim objMO As Management.ManagementObject

Execute the query.

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

Dispose object variables.

objMOS.Dispose()

objMOS = Nothing

objMO.Dispose()

objMO = Nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM