14

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

3
  • @Mitch: Overusing LMGTFY ? :) Commented Jan 7, 2010 at 8:07
  • Although I'm pretty sure the answer is web app which I think renders the two answers so far useless to you. Commented Jan 7, 2010 at 8:15
  • did you figured that out? if yes please share how you get the client's mac address, I need to do the same Commented Dec 4, 2013 at 9:19

3 Answers 3

13

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
Sign up to request clarification or add additional context in comments.

1 Comment

Had to check for CBool(mo("ipEnabled")) before getting the IP address because I was getting a null exception. I have about 10 mo's that are not ipEnabled and do not have a mac address. Adding the condition only gets the ones with a mac address.
6

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

1 Comment

Tested it and gave me server machine.
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

1 Comment

Dim objObject As Object aaaaargh, hungarian notation makes baby jesus cry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.