|
-
June 1st, 2006, 07:32 PM
#1
get MAC and IP of my NIC card
I am trying to find the IP address associated with a particular NIC card. If a PC has more than 1 NIC card, then it can have more than 1 IP address.
So... given a particular IP address, how can I find the MAC (or NIC card) associated with that IP?
Here is what I have so far... its close, but not quite there.
Code:
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
//could have multiple MAC addresses
lstOutput.Items.Add("MAC : " + adapter.GetPhysicalAddress());
}
lstOutput.Items.Add("Host Name : " + Dns.GetHostName());
IPAddress[] hosts = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress address in hosts)
{
//could have multiple IPs (how do I associate with the correct MAC?)
lstOutput.Items.Add("Address : " + address.ToString());
}
-
June 2nd, 2006, 01:47 AM
#2
Re: get MAC and IP of my NIC card
-
June 2nd, 2006, 10:22 AM
#3
Re: get MAC and IP of my NIC card
Not sure how that article relates to my question. As you can see, I already have the MAC address. I am looking for a method to relate the MAC to the IP.
Also, that article is native c++. My code is in c# (and so is this forum).
-
June 2nd, 2006, 11:00 PM
#4
Re: get MAC and IP of my NIC card
u can get the ip and use it to get the MACAddress
Code:
public string GetMacAddress(string IPAddress)
{
string strMacAddress = string.Empty ;
try
{
string strTempMacAddress= string.Empty ;
ProcessStartInfo objProcessStartInfo = new ProcessStartInfo();
Process objProcess = new Process();
objProcessStartInfo.FileName = "nbtstat";
objProcessStartInfo.RedirectStandardInput = false;
objProcessStartInfo.RedirectStandardOutput = true;
objProcessStartInfo.Arguments = "-A " + IPAddress;
objProcessStartInfo.UseShellExecute = false;
objProcess = Process.Start(objProcessStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = strTempMacAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
strTempMacAddress = objProcess.StandardOutput.ReadLine();
}
objProcess.WaitForExit();
strMacAddress = strTempMacAddress.Trim();
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
Console.ReadLine();
}
return strMacAddress;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|