Problem
I’m running a server and would like to see my own IP address displayed.
What is the syntax for obtaining the computer’s own (internal, if possible) IP address?
The following code was written by someone.
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
return localIP;
However, I have a general mistrust of the author, and I am baffled by this code. Is there a better way to accomplish this?
Asked by Nefzen
Solution #1
No, that isn’t the greatest way to go about it. Because a machine may have multiple IP addresses, you must cycle through the list to find the correct one.
Edit: The only thing I would modify is the following:
if (ip.AddressFamily.ToString() == "InterNetwork")
to this:
if (ip.AddressFamily == AddressFamily.InterNetwork)
For comparison, there is no need to ToString an enumeration.
Answered by Andrew Hare
Solution #2
The only way to find out your public IP address is to ask someone else; this code may be useful:
public string GetPublicIP()
{
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
return direction;
}
Answered by ezgar
Solution #3
All-in-one cleaner and solution:D
//This returns the first IP4 address or null
return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
Answered by Mohammed A. Fadil
Solution #4
If you can’t rely on a DNS server to acquire your IP address (which occurred to me), you can use the following method:
The NetworkInterface class in the System.Net.NetworkInformation namespace has a static GetAllNetworkInterfaces function.
Even if you simply have a wireless adapter and/or an ethernet adapter hardware installed on your machine, this method will return all “network interfaces” on your machine, which are usually quite a few. For your local machine, all of these network interfaces have legitimate IP addresses, albeit you’ll probably only use one.
If you’re looking for a specific IP address, you’ll need to narrow the list until you find it. You’ll definitely have to experiment a little, but I had success with the following strategy:
The GetIPProperties method returns an IPInterfaceProperties object for each NetworkInterface, and the UnicastAddresses property returns a list of UnicastIPAddressInformation objects from an IPInterfaceProperties object.
I take the address of the first (if any) unicast address that matches all of these filters at this point.
[code updated on May 16, 2018 to reflect the duplicate address detection state and desired lifetime criteria specified in the text above]
Filtering based on operational status, address family (excluding the loopback address (127.0.0.1), duplicate address detection state, and preferred lifetime as demonstrated in the sample below.
static IEnumerable<IPAddress> GetLocalIpAddresses()
{
// Get the list of network interfaces for the local computer.
var adapters = NetworkInterface.GetAllNetworkInterfaces();
// Return the list of local IPv4 addresses excluding the local
// host, disconnected, and virtual addresses.
return (from adapter in adapters
let properties = adapter.GetIPProperties()
from address in properties.UnicastAddresses
where adapter.OperationalStatus == OperationalStatus.Up &&
address.Address.AddressFamily == AddressFamily.InterNetwork &&
!address.Equals(IPAddress.Loopback) &&
address.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred &&
address.AddressPreferredLifetime != UInt32.MaxValue
select address.Address);
}
Answered by Dr. Wily’s Apprentice
Solution #5
WebClient webClient = new WebClient();
string IP = webClient.DownloadString("http://myip.ozymo.com/");
Answered by James
Post is based on https://stackoverflow.com/questions/1069103/how-to-get-the-ip-address-of-the-server-on-which-my-c-sharp-application-is-runni