Home All Groups Group Topic Archive Search About
Author
30 Sep 2005 4:45 PM
Mark Cooper
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

Author
30 Sep 2005 4:50 PM
Jon Skeet [C# MVP]
Mark Cooper <MarkCnoSpamAlias@newsgroup.nospam> wrote:
> 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
30 Sep 2005 5:20 PM
Mark Cooper
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.
Author
30 Sep 2005 5:46 PM
Jon Skeet [C# MVP]
Mark Cooper <MarkCnoSpamAlias@newsgroup.nospam> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
30 Sep 2005 8:53 PM
Kevin Spencer
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.

Show quoteHide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1da78a50c198734098c830@msnews.microsoft.com...
> Mark Cooper <MarkCnoSpamAlias@newsgroup.nospam> wrote:
>> 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.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too

Bookmark and Share