1

On my web application I am using the following function to get System IP

Function

public void SetHostid()
    {
        try
        {
            string ip = "";
            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();

            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

            IPAddress[] addr = ipEntry.AddressList;

            ip = addr[1].MapToIPv4().ToString();

            HostId = ip;
            HttpContext.Current.Session["Hostid"] = HostId;
        }
        catch (Exception ex)
        {
            Error_ManagerClass em = new Error_ManagerClass();
            em.WriteError(ex);
        }
    }

It works perfectly because the IP is on the 1 postion of the variable addr (addr[ 1]). enter image description here

And the problem comes when I try to run the same solution from a different system. function throws an error while trynig to convert IP to string( ip = addr[1].MapToIPv4().ToString(); ) because IP is not in the position number 1.
enter image description here

how can I change the function to work on every computer ??

7
  • There is a host file in the c:\windows folder that contains the list of IP. In your case you can use regex to get the IP that matches X.X.X.X. Commented Sep 18, 2017 at 6:53
  • 1
    "to get System IP" ... which one? I understand you want an IPv4 address. But looking at your 2nd example: which one? What should happen on Systems that only support IPv6 ? Commented Sep 18, 2017 at 6:53
  • 1
    if the host has multiple IP adresses, how do you decide which one to take? Commented Sep 18, 2017 at 6:53
  • Depending on a specific item of an array is surely not the good idea. You need to loop thru the array of IPAddress and check it's property IsIPv4MappedToIPv6 before calling MapToIpv4 method. Commented Sep 18, 2017 at 6:54
  • 2
    The issue is a bit more complicated than "Get my IP-Address". If you want it to "work on every computer" you need to take into account that a computer can have multiple network interfaces (e.g. Ethernet, Wifi, Cell, etc.) and every interface can potentially multiple addresses. A good first start would be to filter potential results with IPAddress.IsLoopback (to get rid of 127.0.0.1 and ::1) and AddressFamily == AddressFamily.InterNetwork to get rid of non-IPv4 Addresses. Commented Sep 18, 2017 at 7:08

1 Answer 1

2

If you want to get IPv4 only use this code:

var addr = ipEntry.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork);

var firstInList = addr.First(); // get first

But you should consider which IP to chose when there are several IP addresses in system.

Sign up to request clarification or add additional context in comments.

2 Comments

Think you need either First or Single to end your LINQ.
@JohnWu, It gets all IPv4 in system, then you should chose needed one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.