views:

731

answers:

2

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

+2  A: 

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();
}
Ricardo Nolde
Get Location of NIC (e.g: PCI Slot 10 (PCI bus 3, device 0, function 0) ) if it returns null it means it is a Virtual NIC and you can discard it.
Aizaz
A: 
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

Aizaz
Here we are getting only valid IP Addresses, But the problem is still there how to get the active IP Address??? Some VM IPV6 throws an error when we call .Address function that returns long.
Aizaz
How to Differentiate between VM IP and this computer IP ??
Aizaz
Q: Why doing subsring ?If IP Address is 192.168.1.1, I just want "192.168.1." and I will manually add range to scan say from 1 to 254
Aizaz
Use WMI script to get this computer IP Address.
Aizaz
Because only WMI (Windows Management Instrumentation) could identify between those.
Aizaz