I’m using IPAddress.TryParse() to parse IP addresses. However, it’s a little too permissive (parsing ‘1’ returns 0.0.0.1). I’d like to limit the input to dotted octet notation. What’s the best way to do this?
(Note: I’m using .NET 2.0)
Edit
Let me clarify:
I’m writing an app that will scan a range of IPs looking for certain devices (basically a port scanner). When the user enters ‘192.168.0.1’ for the starting address, I want to automatically fill in ‘192.168.0.255’ as the ending address. The problem is that when they type ‘1’, it parses as ‘0.0.0.1’ and the ending address fills in as ‘0.0.0.255’ – which looks goofy.
If you are interested in parsing the format, then I’d use a regular expression. Here’s a good one (source):
That regex doesn’t catch invalid IPs but does enforce your pattern. The TryParse checks their validity.