IPAddress

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Is there a class with a method in the .NET Framework that will verify if a
string is in the correct dotted format to be an IPAddress. Similar to
inet_addr

nnn.nnn.nnn.nnn

Thank You
Mark
 
Mark Cooper said:
Is there a class with a method in the .NET Framework that will verify if a
string is in the correct dotted format to be an IPAddress. Similar to
inet_addr

nnn.nnn.nnn.nnn

Have you tried IPAddress.Parse?
 
Yes but I was hoping to check the string without having to add the overhead
of an exception. The majority of the time it will be a DNS host name not an
IP address.
 
Mark Cooper said:
Yes but I was hoping to check the string without having to add the overhead
of an exception. The majority of the time it will be a DNS host name not an
IP address.

When you talk about the overhead of the exception, just how many of
these are you doing? See
http://www.pobox.com/~skeet/csharp/exceptions.html

It would be reasonably easy to hard-code a test, and feasible to do as
a regular expression (the tricky bit is rejecting 300.300.300.300 but
allowing 30.30.30.255 etc). The *simplest* way is probably to use
IPAddress.Parse though. Whether this counts as an abuse of exceptions
is a matter of taste IMO. Note that in .NET 2 you'll be able to use
IPAddress.TryParse, which is much more what you're after.
 
The 2.0 Platform has a TryParse for the IPAddress, and a number of other
classes as well. It doesn't throw an exception. Unfortunately, the 1.1
platform does not.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
Back
Top