Connecting Tech Pros Worldwide Help | Site Map

getting a mac address

Dan
Guest
 
Posts: n/a
#1: Mar 23 '06
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
scott blood
Guest
 
Posts: n/a
#2: Mar 23 '06

re: getting a mac address


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" <ds23@NOSPAMyork.ac.uk> wrote in message
news:uj1kvAnTGHA.4300@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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[/color]


pkaeowic
Guest
 
Posts: n/a
#3: Mar 23 '06

re: getting a mac address


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
Willy Denoyette [MVP]
Guest
 
Posts: n/a
#4: Mar 23 '06

re: getting a mac address


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" <pkaeowic@discussions.microsoft.com> wrote in message
news:A379EEF7-1FC4-4D1D-8113-B15A24A8D2F7@microsoft.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


Closed Thread