Share via


How to get local machine ip address using C#?

Question

Tuesday, October 4, 2011 9:01 AM

e.g. FooA runs at ComputerA, how can FooA know ComputerA ip address (not localhost/127.0.0.1)?  

Any sample would be appreciated.

Thanks.

All replies (9)

Tuesday, October 4, 2011 9:09 AM ✅Answered

     Console.WriteLine(Dns.GetHostName());
        IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
        foreach (IPAddress addr in localIPs)
        {
            Console.WriteLine(addr);
        }

Tuesday, October 4, 2011 9:13 AM ✅Answered | 2 votes

System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()

returns all your network interfaces.

Call GetIPProperties() on each one to get the properties of the interface, including its UnicastAddresses and MulticastAddresses.


Tuesday, October 4, 2011 9:39 AM ✅Answered | 3 votes

Console.WriteLine(Dns.GetHostName());
        IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
        foreach (IPAddress addr in localIPs)
        {
            if (addr.AddressFamily == AddressFamily.InterNetwork)
            {
                Console.WriteLine(addr);
            }
        }

Tuesday, October 4, 2011 9:06 AM | 2 votes

The only way to know your public IP is to ask someone else to tell you; this code may help you:

 

 public string getPublicIP() {
string direction;
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
WebResponse response = request.GetResponse();
 StreamReader stream = new StreamReader(response.GetResponseStream());
 direction = stream.ReadToEnd();
stream.Close();
response.Close(); //Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("");
direction = direction.Substring(first, last - first);
return direction;
}

 


Tuesday, October 4, 2011 9:30 AM

The returned array including IPv6, e.g. the following,

Dns.GetHostAddresses(Dns.GetHostName())

{System.Net.IPAddress[4]}

[0]: {fe80::1111:3333:2222:d08f%11}

[1]: {fe80::1234:11.178.192.99%12}

[2]: {2099:4898:0:fff:0:1234:11.178.192.99}

[3]: {11.178.192.99}

 

Is there a way only returning ipv4? or can I assume the last one is always the ipv4? Or even there is another best approach?

Thanks.

 


Tuesday, October 4, 2011 9:50 AM | 6 votes

Thanks all.

This also works,

IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First();
Thanks. 

Wednesday, October 5, 2011 6:33 AM

Thank you all for your active participation in the MSDN forum!

Hi Wood,
Welcome to the MSDN forum!

If you have any questions in the future programming, you are welcome to post them in the forum.

Have a nice day!Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Thursday, July 13, 2017 1:02 PM

Thanks


Saturday, April 20, 2019 11:31 PM

Sorry. This should is not the best answer. This gives you a list of addresses with no clue as to what they are. If you are looking for your local network address, you will not be able to identify it from this list. Not useful.