Question

I want to get the current IP address of the computer that has say 3 virtual machines (VM Ware) installed. I want to get LAN address of that computer.

current code that i have returns me an array but how to identify current computer lan address ?

public static string getThisCompIPAddress()
    {
        IPAddress[] addresslist = Dns.GetHostAddresses(Dns.GetHostName());
        return (addresslist[0].ToString());


    }

addresslist returns an array of 3 IP addresses

Was it helpful?

Solution 3

public static ArrayList getThisCompIPAddress()
    {
        ArrayList strArrIpAdrs = new ArrayList();
        ArrayList srtIPAdrsToReturn = new ArrayList();
        addresslist = Dns.GetHostAddresses(Dns.GetHostName());
        for (int i = 0; i < addresslist.Length; i++)
        {
            try
            {
                long ip = addresslist[i].Address;
                strArrIpAdrs.Add(addresslist[i]);
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex.Message);
            }

        }

        foreach (IPAddress ipad in strArrIpAdrs)
        {

            lastIndexOfDot = ipad.ToString().LastIndexOf('.');
            substring = ipad.ToString().Substring(0, ++lastIndexOfDot);
            if (!(srtIPAdrsToReturn.Contains(substring)) && !(substring.Equals("")))
            {
                srtIPAdrsToReturn.Add(substring);
            }
        }

        return srtIPAdrsToReturn;
    }

this is 100% working, the real problem was that it was throwing error while calculating Address that returns long. Error Code is 10045

OTHER TIPS

You could try the NetworkInterface class, and try to match the name or physical address of the LAN connection to find out the real one. Maybe searching within this class and it's members you can find something that suits your needs.

Here is a simple method to provide some usage info:

using System.Net.NetworkInformation;

...

static void ViewNetworkInfo()
{
    NetworkInterface[] networks = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface nw in networks)
    {
        Console.WriteLine(nw.Name);
        Console.WriteLine(nw.GetPhysicalAddress().ToString());

        IPInterfaceProperties ipProps = nw.GetIPProperties();
        foreach (UnicastIPAddressInformation ucip in ipProps.UnicastAddresses)
        {  
            Console.WriteLine(ucip.Address.ToString());
        } 

        Console.WriteLine();
     }
     Console.ReadKey();
}

I've tried all of the solutions above but couldn't get the ip from my "real" machine and not the virtual one. I've managed to use this to get the IP from my virtual machine:

IPAddress[] addresslist = Dns.GetHostAddresses(Environment.ExpandEnvironmentVariables("%CLIENTNAME%"));

The reason why I used it this way is that the function Dns.GetHostAddresses return the adresses of the given host, so if you use the Dns.GetHostName() function it will return the virtual-machine name and not the local machine, but using the name of the machine where you can find using the: Environment.ExpandEnvironmentVariables("%CLIENTNAME%") you can get the client name and not the virtual-machine name, this way you can get the real IP of your local machine.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top