CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    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());
    }

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: get MAC and IP of my NIC card


  3. #3
    Join Date
    May 2003
    Location
    Corvallis, OR
    Posts
    315

    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).

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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
  •  





Click Here to Expand Forum to Full Width

Featured