getting a mac address

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

Hi

Does anyone know how to get the Mac address of a remote PC, using C#? I
believe it's possible by deconstructing the packet received from a ping
but I don't know how to do it.


TIA
Dan
 
Dan,

Try the following code.

protected string GetMACAddressByIP(string ip)
{
try
{
System.Management.ManagementObjectSearcher query= new
System.Management.ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection queryCollection =
query.Get();

bool Found = false;

foreach(System.Management.ManagementObject mo in queryCollection)
{
if(mo["IPAddress"] != null)
{

string temp;
temp = string.Join(".",(string[])mo["IPAddress"] );
if(!temp.Equals(""))
{
if(!ip.Equals(""))
{
if(temp.Equals(ip.Trim()))
{
Found = true;
}

}
else
{
Found = true;
}
}

if(Found == true)
{
if(mo["macaddress"] != null)
{
if(!mo["macaddress"].Equals(""))
{
return (string)mo["macaddress"];
}
}
}
else
{
Found = false;
}

}
}

MessageBox.Show("No Mac Address Found");
return "";

}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}

Regards
Scott Blood
C# Developer
 
Scott's code looks promising...

But keep in mind that you will only be able to get the MAC address of
computers on the local network since that code looks like it's using the ARP
cache on the local machine or where ever the code is running.

As an example, you can not get the MAC address of a computer if you have to
go through a router.

If you're looking to get a MAC address of Windows PCs and you have the
username and password, you can check out the getmac command in the command
prompt. This command will be able to get the MAC address of computers across
routers since it doesn't use the ARP cache. It probably does a remote
procedure call to the Windows PC.

-Pat
 
Not at all, the code as is gets the MAC address of the local NIC's that are
IP enabled, but you can modify the code to query a remote
server/workstation, all you need are appropriate permissions to access the
remote system (just as getmac needs it).

Willy.

| Scott's code looks promising...
|
| But keep in mind that you will only be able to get the MAC address of
| computers on the local network since that code looks like it's using the
ARP
| cache on the local machine or where ever the code is running.
|
| As an example, you can not get the MAC address of a computer if you have
to
| go through a router.
|
| If you're looking to get a MAC address of Windows PCs and you have the
| username and password, you can check out the getmac command in the command
| prompt. This command will be able to get the MAC address of computers
across
| routers since it doesn't use the ARP cache. It probably does a remote
| procedure call to the Windows PC.
|
| -Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top