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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x