0 out of 1 rated this helpful - Rate this topic

IPAddress.TryParse Method

Determines whether a string is a valid IP address.

Namespace:  System.Net
Assembly:  System (in System.dll)
public static bool TryParse(
	string ipString,
	out IPAddress address
)

Parameters

ipString
Type: System.String
The string to validate.
address
Type: System.Net.IPAddress%
The IPAddress version of the string.

Return Value

Type: System.Boolean
true if ipString is a valid IP address; otherwise, false.
Exception Condition
ArgumentException

ipString can't contain Unicode characters.

ArgumentNullException

ipString is null.

The TryParse(String, IPAddress) method accepts octal, decimal, and hexadecimal number formats in the ipString parameter for an IPv4 address, even when mixed. The TryParse(String, IPAddress) method can't handle Unicode characters in the ipString parameter. An application should use caution to filter out Unicode characters.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Is that correct?
After the code below, mybytes will contain: 192, 0, 0, 168 !! That dosent seem correct!? Should it not contain 192, 168, 0, 0 ??

TryParse(
string ipString,
out IPAddress address
TryParse(
string ipString,
out IPAddress address

IPAddress myip;
IPAddress.TryParse("192.168", out myip);
Byte[] mybytes = myip.GetAddressBytes();

IPAddress.TryParse uses inet_addr to parse an IPv4 address

IPAddress.TryParse uses inet_addr under the hood to parse an IPv4 address. This makes the following rules valid:

1) Each part of the address can be either specified in decimal, octal or hexadecimal notation. A leading zero will indicate an octal number, a "0x" prefix will indicate an hexadecimal one. The following strings will all parse to the same address:

"16.16.16.16" // decimal
"020.020.020.020" // octal
"0x10.0x10.0x10.0x10" // hexadecimal
"16.020.0x10.16" // mixed base

2) Compatibility with Berkley software:

When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most 2 bytes of the network address.

Example: "12.12.258" is valid and maps to 12.12.1.2

When a two-part address is specified, the last part is interpreted as a 24-bit quantity and placed in the right-most 3 bytes of the network address.

Example: "12.258" is valid and maps to 12.0.1.2

For further information, read the MSDN article on inet_addr.

Unexpected ArgumentNullException
Hello, $0$0 $0 $0I was also bit by the unexpected ArgumentNullException when ipAddress is null. None of the other TryParse methods do this, as far as I can see. At a minimum this needs to be documented so it shows up with Intellisense.$0 $0$0 $0 $0Regards,$0 $0$0 $0 $0Daniel Lidström$0
TryParse throws an exception if ipString is null
I didn't expect a TryParse method to ever throw an exception, but it does:

System.ArgumentNullException was unhandled by user code
  Message=Value cannot be null.
Parameter name: ipString
  Source=System
  ParamName=ipString
  StackTrace:
       at System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)
       at System.Net.IPAddress.TryParse(String ipString, IPAddress& address)
TryParse Sample Using PowerShell
<#
.SYNOPSIS
Demonstrates use of the GetHostEntry method of System.Net.DNS Class
.DESCRIPTION
This script is a an MSDN sample, using PowerShell. This script
uses the IP address of the host to find.
.NOTES
File Name : Get-HostEntry2.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
Sample posted to:
http://pshscripts.blogspot.com/
Original MSDN sample at:
http://msdn.microsoft.com/en-us/library/ms143997.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-HostEntry2.ps1 10.10.1.117
Host Name : cookham11.cookham.net
Alias :
Address : 10.10.1.117
.PARAM HostName
The name of the host to search for - the default is "127.0.0.1"
#>

param (
[string] $HostAddress = "127.0.0.1"
)

###
# Start of Script
###

# Convert $hostaddres to IPaddress class.

# Create one for next call
$HostIp = [System.Net.IPAddress]::Parse("127.0.0.1")
if (! ([system.Net.IPAddress]::TryParse($hostaddress, [ref] $HostIP))) {"Not valid IP address"; return}

# Get Host info
$hostentrydetails = [System.Net.Dns]::GetHostEntry($HostIP)

# Print details:
"Host Name : {0}" -f $hostentrydetails.HostName
foreach ($alias in $hostentrydetails.alises) {
"Alias : {0}" -f $alias
}
foreach ($addr in $hostentrydetails.addresslist) {
"Address : {0}" -f $Addr.ipaddresstostring
}

# End of script
This method requires better documentation.
What happens with second parameter when this method returns false?

[tfl 4 Oct 2010]
If the string is not an IP address, then the method returns false. In that case, the return value is null. You can demostrate it by using the PowerShell script sample posted here and playing around with the address used.