Determining the IP address of your device

Device's do not typically register a name with the network.  This means you cannot just use a name to connect to or ping the device.  Back in July 2004, I posted a snippet that shows all of the IP addresses (IPv4 and IPv6) that equate to "localhost".  Today, I am posting a slightly modified version that displays the IP addresses that you can use to connect to your device.

Please note: I intentionally did not make a user interface to keep the snippet as small as possible.  Also, since many devices do not come with a command prompt, I did not use Console.WriteLine.

using System;
using System.Net;
using System.Windows.Forms;

class WhoAmI
{
    public static void Main()
    {
        try
        {
            IPHostEntry he = Dns.Resolve(Dns.GetHostName());
            foreach(IPAddress addr in he.AddressList)
            {
                MessageBox.Show(addr.ToString());
            }
        }
        catch(Exception e)
        {
            MessageBox.Show(String.Format("Caught: {0}\r\n{1}", e.GetType().ToString(), e.Message));
        }
    }
}

That's all there is to it!  I use code based on the above when running the Windows Mobile emulator as a standalone application (with ethernet enabled).

Enjoy!
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.