March 27, 2006

Safely Parse An Ip Address

This helper method safely parses an IP address from a string using the System.Net.IPAddress class
private static string SafeIpAddressFromString(string value)
{
  if (ReferenceEquals(value, null))
      value = ""; // System.Net.IPAddress.Parse cannot handle null values
  System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse(value);
  return ipAddr.ToString();
}

No comments: