How to get MAC address of client machine in c# and vb.net
-
@Mitch: Overusing LMGTFY ? :)Madi D.– Madi D.2010-01-07 08:07:22 +00:00Commented 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.MartW– MartW2010-01-07 08:15:40 +00:00Commented 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 sameAlex– Alex2013-12-04 09:19:36 +00:00Commented Dec 4, 2013 at 9:19
Add a comment
|
3 Answers
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
1 Comment
NTR
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.
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
NTR
Tested it and gave me server machine.
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
Paul Creasey
Dim objObject As Object aaaaargh, hungarian notation makes baby jesus cry