IPAddress.Parse Método

Definición

Sobrecargas

Parse(String)

Convierte una cadena de dirección IP en una instancia de IPAddress.

Parse(ReadOnlySpan<Char>)

Convierte una dirección IP representada como un intervalo de caracteres en una instancia de IPAddress.

Parse(String)

Source:
IPAddress.cs
Source:
IPAddress.cs
Source:
IPAddress.cs

Convierte una cadena de dirección IP en una instancia de IPAddress.

public:
 static System::Net::IPAddress ^ Parse(System::String ^ ipString);
public static System.Net.IPAddress Parse (string ipString);
static member Parse : string -> System.Net.IPAddress
Public Shared Function Parse (ipString As String) As IPAddress

Parámetros

ipString
String

Cadena que contiene una dirección IP en notación de cuatro números separados por puntos para IPv4 y en notación hexadecimal con dos puntos para IPv6.

Devoluciones

Instancia de IPAddress.

Excepciones

ipString es null.

El valor de ipString no es una dirección IP válida.

Ejemplos

El código siguiente convierte una cadena que contiene una dirección IP, en notación punto-cuadrátil para IPv4 o en notación hexadecimal de dos puntos para IPv6, en una instancia de la IPAddress clase . A continuación, usa el método sobrecargado ToString para mostrar la dirección en notación estándar.

#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 );
}

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);
    }
   }
}
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
    
   
   ' 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
End Class

Comentarios

El método estático Parse crea una IPAddress instancia a partir de una dirección IP expresada en notación punto-cuadrático para IPv4 y en notación hexadecimal de dos puntos para IPv6.

El número de partes (cada parte está separada por un punto) en ipString determina cómo se construye la dirección IP. Una dirección de una parte se almacena directamente en la dirección de red. Una dirección de dos partes, conveniente para especificar una dirección A de clase, coloca la parte inicial en el primer byte y la parte final en los tres bytes más a la derecha de la dirección de red. Una dirección de tres partes, conveniente para especificar una dirección de clase B, coloca la primera parte en el primer byte, la segunda parte del segundo byte y la parte final en los dos bytes más a la derecha de la dirección de red. Por ejemplo:

Número de partes y ejemplo ipString Dirección IPv4 para IPAddress
1 -- "65535" 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
4 -- "1.1.1.10" 1.1.1.10
4 -- "1.1.1.010" 1.1.1.8
1 - "0x2F" 0.0.0.47

Se aplica a

Parse(ReadOnlySpan<Char>)

Source:
IPAddress.cs
Source:
IPAddress.cs
Source:
IPAddress.cs

Convierte una dirección IP representada como un intervalo de caracteres en una instancia de IPAddress.

public:
 static System::Net::IPAddress ^ Parse(ReadOnlySpan<char> ipSpan);
public:
 static System::Net::IPAddress ^ Parse(ReadOnlySpan<char> ipString);
public static System.Net.IPAddress Parse (ReadOnlySpan<char> ipSpan);
public static System.Net.IPAddress Parse (ReadOnlySpan<char> ipString);
static member Parse : ReadOnlySpan<char> -> System.Net.IPAddress
static member Parse : ReadOnlySpan<char> -> System.Net.IPAddress
Public Shared Function Parse (ipSpan As ReadOnlySpan(Of Char)) As IPAddress
Public Shared Function Parse (ipString As ReadOnlySpan(Of Char)) As IPAddress

Parámetros

ipStringipSpan
ReadOnlySpan<Char>

Intervalo de caracteres que contiene una dirección IP en notación de cuatro números separados por puntos para IPv4 y en notación hexadecimal con dos puntos para IPv6.

Devoluciones

Dirección IP convertida.

Excepciones

El valor de ipString no es una dirección IP válida.

Se aplica a