How do I get the IP address of a machine in C#?
Asked
Active
Viewed 6.7k times
14
-
2then you could at least link to the article and tell us what's wrong with it. And what's wrong with loops by the way? :) – Gerrie Schenck Jan 07 '10 at 10:15
-
3As phrased, "127.0.0.1" is a correct answer. It's an IP address, of the current machine. – MSalters Jan 07 '10 at 10:21
-
1@MSalters: I'm still not sure if `return 127.0.0.1` would be an answer I'd upvote :) – marcgg Jan 07 '10 at 11:33
-
@hobodave, maybe he just thought it would be a useful question and answer to have on StackOverflow - the more questions and answers there are here the more useful it is, no? – Zann Anderson Nov 30 '10 at 17:34
-
2@ArielBH, I just googled ".net get ip address of current machine" and got this question. Your comment is less than useless. – Chris Marasti-Georg Jun 25 '14 at 17:31
-
Follow the answer in this link. It works for me. [Stack Overflow Get IP Address](http://stackoverflow.com/questions/21669186/ipaddress-of-a-login-system/39524251#39524251) – Zia Ul Mustafa Sep 16 '16 at 05:53
5 Answers
33
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
Your machine doesn't have a single IP address, and some of the returned addresses can be IPv6.
MSDN links:
Alternatively, as MSalters mentioned, 127.0.0.1 / ::1 is the loopback address and will always refer to the local machine. For obvious reasons, however, it cannot be used to connect to the local machine from a remote machine.
Community
- 1
- 1
Richard Szalay
- 81,010
- 19
- 172
- 232
-
3Copied from @patridge in the other answer: If you are looking for a more relevant IP address, you may want to exclude loopback IPs (e.g., 127.0.0.1 and ::1) with something like this: .Where(ip => !Net.IPAddress.IsLoopback(ip)) – Chris Marasti-Georg Jun 25 '14 at 18:48
10
My desired answer was
string ipAddress = "";
if (Dns.GetHostAddresses(Dns.GetHostName()).Length > 0)
{
ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
}
Azhar
- 19,874
- 38
- 137
- 207
-
10This is executing `GetHostAddresses` and `GetHostName` twice; you should assign the results of GetHostAddresses to a variable and then check the `Length`. – Richard Szalay Jan 11 '10 at 10:26
-
9If you are looking for a more relevant IP address, you may want to exclude loopback IPs (e.g., 127.0.0.1 and ::1) with something like this: `.Where(ip => !Net.IPAddress.IsLoopback(ip))`. – patridge Mar 15 '10 at 17:12
1
IPHostEntry ip = DNS.GetHostByName (strHostName);
IPAddress [] IPaddr = ip.AddressList;
for (int i = 0; i < IPaddr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IPaddr[i].ToString ());
}
Gopi
- 5,316
- 20
- 74
- 136
-
5GetHostByName is deprecated - http://msdn.microsoft.com/en-us/library/system.net.dns.gethostbyname.aspx – Richard Szalay Jan 07 '10 at 10:04
0
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
// Get the IP
string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
//use Following Namespace- using System.Net;
Ranjeet
- 171
- 1
- 2
- 16
-1
this worked in my case, anyone looking for a simple way, must try this syntax. Thanks
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
Console.WriteLine(hostName);
// Get the IP
string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
Console.WriteLine("My IP Address is :"+myIP);
Console.ReadKey();
DotNet Team
- 3,608
- 18
- 30
-
This is identical to every other answer. This only adds noise and helps nobody. – Panagiotis Kanavos Mar 10 '22 at 07:52