0

How can I get the IP address from a string such as '\\myserver'?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MartinHT
  • 289
  • 1
  • 9

2 Answers2

0

Use gethostbyname() or getaddrinfo().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

This is my solution:

public static System.Net.IPAddress GetRemoteIpV4Address(string hostNameOrIpAddress)
{
  if(!string.IsNullOrWhiteSpace(hostNameOrIpAddress))
  {
    return System.Net.Dns.GetHostEntry(hostNameOrIpAddress).AddressList.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) ??
      new System.Net.IPAddress(new byte[] { 147, 0, 0, 1 });
  }
  return null;
}
MartinHT
  • 289
  • 1
  • 9