CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Mar 2005
    Posts
    99

    Find LAN IP amongst Virtual IPs

    I'm writing a .Net 2.0 application in C#. Given a computer name, I want to look up the IP address for that machine. If there are IPv6 and IPv4 addresses, I want to return the IPv4 address. However I recently received a bug report that when there are Virtual Machine instances installed, the IP address that comes back is the VM's IP and not the LAN IP of the computer.

    Is there any way to distinguish between a VM IP and a LAN IP if both are IPv4 format?

    Code:
    public string GetIPAddress(string PCName)
    {
         string strRet = String.Empty;
         foreach (IPAddress adr in Dns.GetHostEntry(PCName).AddressList)
         {
              // Sets any IPv4 addresses to the return string
              if (adr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
              {
                   strRet = adr.ToString();
                   break;//Fine if no VM installed, but otherwise gets the wrong IP
              }
         }
         return strRet;
    }
    Last edited by nice_guy_mel; March 31st, 2010 at 10:40 AM.

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