Friday, July 12, 2013

WPF - Different ways to get System IP Address

Following method get the local from the list of Addresses as it has more IP address if it has more adapters

private object GetLocalIPAddressFromAddressList()
        {
            var hostName = Dns.GetHostName();
            var ipEntry = Dns.GetHostEntry(hostName);

            var result = from address in ipEntry.AddressList
                    where address.AddressFamily == AddressFamily.InterNetwork
                    select address;
            return result.LastOrDefault();

        }


Using Ping method to get the IP Address, it helpful to find others IP address also

        public IPAddress GetIPAddressUsingPing()
        {
            string hostName = "rbalajiprasad.blogspot.com";
            //You can get local Computer IP address also by sending HostName by following
            //string hostName = System.Net.Dns.GetHostName();


            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

1 comment:

  1. We have a slight change in Java to fetch the System's IP address. Anyways, this code is very useful for me. I'm making a project that's uses a module which is to fetch the IP address. Next module will change the IP address. Hope I'll code that.

    Regards,
    Silvester Norman
    Change MAC Address

    ReplyDelete