473,466 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Retreiving the correct MacAddress using the corresponding IP address

Hi,

I want to to find the Mac address of the NIC being used by a given
process using a corresponding IP address. It should be pointed out
that the machines that this program will be running on will be using
more then one NIC. SO I have to use the IP adress to get the correct
MAC. Here is the code I'm using to get the IP. Any help would be
greatly appreciated, and thank you in advance.

public static IPAddress GetLocalIP()
{
IPAddress localIp = null;
String hostName = Dns.GetHostName();
IPHostEntry ipHostEntry = Dns.GetHostByName(hostName);

if( ipHostEntry.AddressList.Length >= 1 )
{
localIp = ipHostEntry.AddressList[0];
}
return localIp;
}

I know I'm just grabbing the first one, but for the purposes of the
program it doesn't really matter. But it does matter that the Mac
Address I grab correspond to this IP I'm grabbing here. Thanks again

Jun 26 '06 #1
1 2150
NeverMind, I figured it out. In case anyone is having the same
problem, this is the solution I came up with.

public static String GetPhysicalAddress(String ipAddress)
{
String[] ipAddresses = null;
String macAddress = null;
ManagementObjectSearcher query = null;
ManagementObjectCollection queryCollection = null;

try
{
query = new ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration") ;
queryCollection = query.Get();

foreach(ManagementObject mo in queryCollection)
{
Console.WriteLine(queryCollection.Count);
if(mo["IpAddress"] != null)
{
ipAddresses = (String[])mo["IPAddress"];

foreach(String ip in ipAddresses)
{
if(ip.CompareTo(ipAddress) == 0)
{
if(mo["MacAddress"] != null)
{
macAddress = (String)mo["MacAddress"];
break;
}
}
}
}
}
}
catch(Exception ex)
{
}
return macAddress;
}

Jun 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guy Erez | last post by:
Hi, I'm running queries with MySql 4.0.17 that return thousands of records. Because I need to present them in GUI, I returieve the results in chunks using LIMIT, for example - get first 100,...
2
by: Toma Marinov | last post by:
Hello ! First, excuse me for my not very good english! >:O( With this code below, I retrieve the MAC address of my network card from WMI class Win32_NetworkAdapter, and add it to ListBox : ...
4
by: akayoku | last post by:
Hello all, I'm working on a school project to make a security program. I need to get the MAC Address from the computer. That is the "Physical Address" when you type "ipconfig/all" in MSDOS...
2
by: Gunnar_Frenzel | last post by:
Hello, this might be an easy question, but I don't have any handy solution at hand. I have an application that is supposed to send UDP broadcast. So far so easy, I did: Socket sockSendBroadcast...
0
by: DKn | last post by:
Hello All, Can anybody help me!!! How to get MAC Address of a Remote System using WMI Classes in C#.Net 2.0...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
6
by: jt | last post by:
#include <stdio.h> void f(); int main() { long int i; i=20; f(); i = 10; printf ("\n%d\n",i);
12
idsanjeev
by: idsanjeev | last post by:
hi frainds i try to catch ip_address,mac_address and hostname of pc that is connected in lan with router or switch. i dont know how to catch hostname of pc and mac_address. i am using code for ip...
4
by: padmapriya | last post by:
I need to create three radio buttons and when the radio button is clicked, the corresponding text fields such as 3 different address types should get activated. If i clicked the first radio button,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.