views:

820

answers:

6

I am looking to figure out what my ip address is from a c# console application. I am used to a web application by using the Request.servervariables() method. But not sure about how to do it in a console app. Any ideas?

Thanks

+2  A: 
using System;
using System.Net;

public class DNSUtility
{
    public static int Main (string [] args)
    {

      String strHostName = new String ("");
      if (args.Length == 0)
      {
          // Getting Ip address of local machine...
          // First get the host name of local machine.
          strHostName = DNS.GetHostName ();
          Console.WriteLine ("Local Machine's Host Name: " +  strHostName);
      }
      else
      {
          strHostName = args[0];
      }

      // Then using host name, get the IP address list..
      IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
      IPAddress [] addr = ipEntry.AddressList;

      for (int i = 0; i < addr.Length; i++)
      {
          Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
      }
      return 0;
    }    
 }

source : http://www.codeproject.com/KB/cs/network.aspx

Yassir
+9  A: 

The easiest way to do this is as follows:

using System;
using System.Net;


namespace ConsoleTest
{
    class Program
    {
        static void Main()
        {
            String strHostName = string.Empty;
            // Getting Ip address of local machine...
            // First get the host name of local machine.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);
            // Then using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }
            Console.ReadLine();
        }
    }
}
Jason Heine
OKay, so I see a lot of answers here but this one seems easy to read. I like what Martin Peck said about having multiple IP addresses, and I think that this here gives me the right solution. I ran this locally and it gave me what I wanted. Thank you so much!
Yes, I agree with martin, you have to watch out for multiple IP Addresses. This code will handle this and you can choose what to do with it from there.
Jason Heine
You should probably include a link to the page you copied this code from, don't you think? I mean, it's one of the first results on Google.
Iceman
You are absolutly right. I just happened to have used this code block before and copied it from there, but the resource that I got it from originally was here: http://www.codeproject.com/KB/cs/network.aspx
Jason Heine
Why do all these answers use hungarian notation for strHostName, but no other variables?
Richard Szalay
+1  A: 

System.Net.Dns.GetHostAddresses() should do it.

Nik
+2  A: 

The System.Net namespace is your friend here. In particular, APIs such as DNS.GetHostByName.

However, any given machine may have multiple IP addresses (multiple NICs, IPv4 and IPv6 etc) so it's not quite as simple a question as you pose.

Martin Peck
I really like your comment for having multiple ip addresses. Based on that the code above worked really well. Thanks!
+2  A: 

Try this:

String strHostName = Dns.GetHostName();

Console.WriteLine("Host Name: " + strHostName);

// Find host by name    IPHostEntry
iphostentry = Dns.GetHostByName(strHostName);

// Enumerate IP addresses
int nIP = 0;   
foreach(IPAddress ipaddress in iphostentry.AddressList) {
   Console.WriteLine("IP #" + ++nIP + ": " + ipaddress.ToString());    
}
Prithis
+1  A: 

IPAddress[] addresslist = Dns.GetHostAddresses(Dns.GetHostName());

Aizaz