16

How do I get the IP address of a machine in C#?

Azhar
  • 20,500
  • 38
  • 146
  • 211
  • 2
    then 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
  • 3
    As 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 Answers5

34
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
  • 83,269
  • 19
  • 178
  • 237
  • 3
    Copied 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
  • 20,500
  • 38
  • 146
  • 211
  • 12
    This 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
  • 9
    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))`. – 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,656
  • 22
  • 80
  • 146
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
  • 181
  • 1
  • 3
  • 17
0
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); //get all network interfaces
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); // get all IP addresses based on the local host name

foreach (NetworkInterface adapter in adapters) //for each Network interface in addapters
{
    IPInterfaceProperties properties = adapter.GetIPProperties(); // get the ip properties from the adapter and store them into properties
    foreach (UnicastIPAddressInformation ip in properties.UnicastAddresses) // for each UnicastIPAddressInformation in the IPInterfaceProperties Unicast address( this assocaites the IP address with the correct adapter)
    {
        //if the operationalStatus of the adapter is up and the ip Address family is in the Internwork
        if ((adapter.Name == "Ethernet" || adapter.Name == "Ethernet 2") && (ip.Address.AddressFamily == AddressFamily.InterNetwork)) //test against the name of the adapter you want to get
        {
            ipAddress = ip.Address.ToString();
                        
        }//end if
    }//end inner for, the UnicastIPAddressInformation for
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Yeager
  • 1