Thursday, May 19, 2011

Get IP Address in C#

Using .NET 1.1 and 2.0 to retrieve the IP address
Well, in .NET 1.1 and 2.0 there are methods in the System.Net namespace that do that. Speaking of System.Net, don't forget to include the using statement, to avoid the long lines I have below:

using System.Net;

// Get the hostname
string myHost = System.Net.Dns.GetHostName();

// Get the IP from the host name
string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

No comments:

Post a Comment