Export (0) Print
Expand All

IPAddress.Parse Method

Converts an IP address string to an IPAddress instance.

[Visual Basic]
Public Shared Function Parse( _
   ByVal ipString As String _
) As IPAddress
[C#]
public static IPAddress Parse(
 string ipString
);
[C++]
public: static IPAddress* Parse(
 String* ipString
);
[JScript]
public static function Parse(
   ipString : String
) : IPAddress;

Parameters

ipString
A string containing an IP address in dotted-quad notation in case of IPv4 and in colon-hexadecimal notation, in case of IPv6.

Return Value

An IPAddress instance.

Exceptions

Exception Type Condition
ArgumentNullException ipString is a null reference (Nothing in Visual Basic).
FormatException ipString is not a valid IP address.

Remarks

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

Example

[Visual Basic, C#, C++] The following code converts a string containing an IP address, in dotted-quad notation for IPv4 or in colon-hexadecimal 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);
    }

   }
}

[C++] 
#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);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

IPAddress Class | IPAddress Members | System.Net Namespace

Show:
© 2014 Microsoft