in

Utah .NET User Group

Home of Utah's professional .NET developers.

How to find an IP address in C#?

Last post 10-28-2008 11:53 AM by miguelito928. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-28-2008 10:21 AM

    • Wayne
    • Top 25 Contributor
    • Joined on 12-07-2006
    • Posts 3

    How to find an IP address in C#?

    I've looked for the past hour but can't find any easy way to get an IP address of the machine for a WinForms app. Is there anything simple like getting the login name like WindowsIndenty.GetCurrent()? Wayne
  • 10-28-2008 11:01 AM In reply to

    Re: How to find an IP address in C#?

    I've got some code for you to get the IP address. Hold on while I look it up...
  • 10-28-2008 11:53 AM In reply to

    Re: How to find an IP address in C#?

    Ok, so you should be able to get it by the host name. Do DNS.GetHostName() and then feed that into DNS.GetHostByName(string hostname) and you will get back an IPHostEntry object. Use the AddressList property of the IPHostEntry object and you will get a collection of IPAddress objects.

    There is another method I used when I was trying to get the IP address of a virtual machine (unfortunately, every VM had the same host name). We had to get the IPAddress directly from the NIC cards. This method is quite a bit more sloppy though. Make this call: NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().UnicastAddresses[0].Address

    Hope this helps.
Page 1 of 1 (3 items)
Copyright © 2000-2007, Utah .NET User Group
Powered by Community Server (Commercial Edition), by Telligent Systems