471,075 Members | 916 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

getting a mac address

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
Mar 23 '06 #1
3 15521
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
"Dan" <ds**@NOSPAMyork.ac.uk> wrote in message
news:uj**************@TK2MSFTNGP14.phx.gbl...
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

Mar 23 '06 #2
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
Mar 23 '06 #3
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.

"pkaeowic" <pk******@discussions.microsoft.com> wrote in message
news:A3**********************************@microsof t.com...
| 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
Mar 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Spidah | last post: by
9 posts views Thread by Marchello | last post: by
3 posts views Thread by Fred Zwarts | last post: by
7 posts views Thread by Prashanth Badabagni | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.