Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
IPAddress Class
IPAddress Methods
 Parse Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
IPAddress..::.Parse Method

Converts an IP address string to an IPAddress instance.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Shared Function Parse ( _
    ipString As String _
) As IPAddress
Visual Basic (Usage)
Dim ipString As String
Dim returnValue As IPAddress

returnValue = IPAddress.Parse(ipString)
C#
public static IPAddress Parse(
    string ipString
)
Visual C++
public:
static IPAddress^ Parse(
    String^ ipString
)
JScript
public static function Parse(
    ipString : String
) : IPAddress

Parameters

ipString
Type: System..::.String
A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

Return Value

Type: System.Net..::.IPAddress
An IPAddress instance.
ExceptionCondition
ArgumentNullException

ipString is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

ipString is not a valid IP address.

The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. A one part address is stored directly in the network address. A two part address, convenient for specifying a class A address, puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. A three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part in the second byte, and the final part in the right-most two bytes of the network address. For example:

Number of parts and example ipString

IPv4 address for IPAddress

1 -- "65536"

0.0.255.255

2 -- "20.2"

20.0.0.2

2 -- "20.65535"

20.0.255.255

3 -- "128.1.2"

128.1.0.2

The following code converts a string that contains an IP address, in dotted-quad notation for IPv4 or in colon-hexadecimal notation for IPv6, into an instance of the IPAddress class. Then it uses the overloaded ToString method to display the address in standard notation.

Visual Basic
Imports System
Imports System.Net



Class ParseAddress

   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      Main(System.Environment.GetCommandLineArgs())
   End Sub


   Overloads Private Shared Sub Main(args() As String)
      Dim IPaddress As String

      If args.Length = 1 Then
         Console.WriteLine("Please enter an IP address.")
         Console.WriteLine("Usage:   >cs_parse any IPv4 or IPv6 address.")
         Console.WriteLine("Example: >cs_parse 127.0.0.1")
         Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1")
         Return
      Else
         IPaddress = args(1)
      End If 
      ' Get the list of the IPv6 addresses associated with the requested host.
      parse(IPaddress)
   End Sub 'Main


   ' This method calls the IPAddress.Parse method to check the ipAddress 
   ' input string. If the ipAddress argument represents a syntatical correct IPv4 or
   ' IPv6 address, the method displays the Parse output into quad-notation or
   ' colon-hexadecimal notation, respectively. Otherwise, it displays an 
   ' error message.
   Private Shared Sub parse(ipAddr As String)
      Try
         ' Create an instance of IPAddress for the specified address string (in 
         ' dotted-quad, or colon-hexadecimal notation).
         Dim address As IPAddress = IPAddress.Parse(ipAddr)

         ' Display the address in standard notation.
         Console.WriteLine(("Parsing your input string: " + """" + ipAddr + """" + " produces this address (shown in its standard notation): " + address.ToString()))

      Catch e As ArgumentNullException
         Console.WriteLine("ArgumentNullException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))

      Catch e As FormatException
         Console.WriteLine("FormatException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))

      Catch e As Exception
         Console.WriteLine("Exception caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      End Try
   End Sub 'parse 
End Class 'ParseAddress
C#
using System;
using System.Net;

class ParseAddress
{

  private static void Main(string[] args) 
  {
    string IPaddress;

    if (args.Length == 0)
    {
      Console.WriteLine("Please enter an IP address.");
      Console.WriteLine("Usage:   >cs_parse any IPv4 or IPv6 address.");
      Console.WriteLine("Example: >cs_parse 127.0.0.1");
      Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1");
      return;
    }
    else
      IPaddress = args[0];

    // Get the list of the IPv6 addresses associated with the requested host.
    parse(IPaddress);

  }

  // This method calls the IPAddress.Parse method to check the ipAddress 
  // input string. If the ipAddress argument represents a syntatically correct IPv4 or
  // IPv6 address, the method displays the Parse output into quad-notation or
  // colon-hexadecimal notation, respectively. Otherwise, it displays an 
  // error message.
  private static void parse(string ipAddress)
  {
    try
    {
      // Create an instance of IPAddress for the specified address string (in 
      // dotted-quad, or colon-hexadecimal notation).
      IPAddress address = IPAddress.Parse(ipAddress);

      // Display the address in standard notation.
      Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());
    }

    catch(ArgumentNullException e)
    {
      Console.WriteLine("ArgumentNullException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

    catch(FormatException e)
    {
      Console.WriteLine("FormatException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

    catch(Exception e)
    {
      Console.WriteLine("Exception caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

   }
}
Visual C++
#using <System.dll>

using namespace System;
using namespace System::Net;

// This method calls the IPAddress::Parse method to check the ipAddress
// input string. If the ipAddress argument represents a syntatically correct IPv4 or
// IPv6 address, the method displays the Parse output into quad-notation or
// colon-hexadecimal notation, respectively. Otherwise, it displays an
// error message.
void parse( String^ ipAddress )
{
   try
   {

      // Create an instance of IPAddress for the specified address string (in
      // dotted-quad, or colon-hexadecimal notation).
      IPAddress^ address = IPAddress::Parse( ipAddress );

      // Display the address in standard notation.
      Console::WriteLine( "Parsing your input string: \"{0}\" produces this address (shown in its standard notation): {1}", ipAddress, address );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException caught!!!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( FormatException^ e ) 
   {
      Console::WriteLine( "FormatException caught!!!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception caught!!!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }

}

int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   String^ IPaddress;
   if ( args->Length == 1 )
   {
      Console::WriteLine( "Please enter an IP address." );
      Console::WriteLine( "Usage:   >cs_parse any IPv4 or IPv6 address." );
      Console::WriteLine( "Example: >cs_parse 127.0.0.1" );
      Console::WriteLine( "Example: >cs_parse 0:0:0:0:0:0:0:1" );
      return 0;
   }
   else
      IPaddress = args[ 1 ];


   // Get the list of the IPv6 addresses associated with the requested host.
   parse( IPaddress );
}

CPP_OLD
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Net;


// This method calls the IPAddress::Parse method to check the ipAddress
// input string. If the ipAddress argument represents a syntatically correct IPv4 or
// IPv6 address, the method displays the Parse output into quad-notation or
// colon-hexadecimal notation, respectively. Otherwise, it displays an
// error message.
void parse(String* ipAddress) {
   try {
      // Create an instance of IPAddress for the specified address string (in
      // dotted-quad, or colon-hexadecimal notation).
      IPAddress* address = IPAddress::Parse(ipAddress);

      // Display the address in standard notation.
      Console::WriteLine(S"Parsing your input string: \"{0}\" produces this address (shown in its standard notation): {1}", ipAddress, address);
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"ArgumentNullException caught!!!");
      Console::WriteLine(S"Source : {0}", e->Source);
      Console::WriteLine(S"Message : {0}", e->Message);
   } catch (FormatException* e) {
      Console::WriteLine(S"FormatException caught!!!");
      Console::WriteLine(S"Source : {0}", e->Source);
      Console::WriteLine(S"Message : {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception caught!!!");
      Console::WriteLine(S"Source : {0}", e->Source);
      Console::WriteLine(S"Message : {0}", e->Message);
   }
}

int main() {
   String* args[] = Environment::GetCommandLineArgs();
   String* IPaddress;

   if (args->Length == 1) {
      Console::WriteLine(S"Please enter an IP address.");
      Console::WriteLine(S"Usage:   >cs_parse any IPv4 or IPv6 address.");
      Console::WriteLine(S"Example: >cs_parse 127.0.0.1");
      Console::WriteLine(S"Example: >cs_parse 0:0:0:0:0:0:0:1");
      return 0;
   } else
      IPaddress = args[1];
   // Get the list of the IPv6 addresses associated with the requested host.
   parse(IPaddress);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
IPAddress.Parse method using PowerShell      Thomas Lee   |   Edit   |   Show History
# get-ParsedIPAddress.ps1
# Demostrates parsing an IP address with PowerShell
# Thomas Lee - tfl@psp.co.uk
 
# Parse an IP address
$ipaddress = "131.107.2.200"
$addr = [system.net.ipaddress]::parse($ipaddress)
 
# Display information on the returned object
$addr | gm
 
# Display IP address results
$addr
""
 
# Display address
"IP address (as Int64) : {0}" -f $addr.address
"Ip Address (as String) : {0}" -f $addr.ipaddresstostring

This script produes the following output:

PS C:\Documents and Settings\LeeT> D:\foo\get-parsedipaddress.ps1
  
TypeName: System.Net.IPAddress
Name              MemberType     Definition                                             
---- ---------- ----------
Equals Method System.Boolean Equals(Object comparand)
GetAddressBytes Method System.Byte[] GetAddressBytes()
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
Address Property System.Int64 Address {get;set;}
AddressFamily Property System.Net.Sockets.AddressFamily AddressFamily {get;}
IsIPv6LinkLocal Property System.Boolean IsIPv6LinkLocal {get;}
IsIPv6Multicast Property System.Boolean IsIPv6Multicast {get;}
IsIPv6SiteLocal Property System.Boolean IsIPv6SiteLocal {get;}
ScopeId Property System.Int64 ScopeId {get;set;}
IPAddressToString ScriptProperty System.Object IPAddressToString {get=$this.Tostring();}
IPAddressToString : 131.107.2.200
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
 
IP address (as Int64)  : 3355601795
Ip Address (as String) : 131.107.2.200
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker