IPAddress.Parse Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Sobrecargas
| Name | Description |
|---|---|
| Parse(String) |
Converte uma cadeia de endereços IP numa IPAddress instância. |
| Parse(ReadOnlySpan<Char>) |
Converte um endereço IP representado como um intervalo de caracteres numa IPAddress instância. |
Parse(String)
Converte uma cadeia de endereços IP numa IPAddress instância.
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
Uma cadeia que contém um endereço IP em notação pontilhada para IPv4 e em notação dois-dois e hexadecimal para IPv6.
Devoluções
Um IPAddress exemplo.
Exceções
ipString é null.
ipString não é um endereço IP válido.
Exemplos
O código seguinte converte uma cadeia que contém um endereço IP, em notação pontilhada para IPv4 ou em notação dois-dois e hexadecimal para IPv6, numa instância da IPAddress classe. Depois, utiliza o método sobrecarregado ToString para mostrar o endereço em notação padrão.
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
Observações
O método estático Parse cria uma IPAddress instância a partir de um endereço IP expresso em notação pontilhada-quad para IPv4 e em notação dois-dois hexadecimal para IPv6.
O número de partes (cada parte é separada por um ponto) determina ipString como o endereço IP é construído. Um endereço de uma parte é armazenado diretamente no endereço de rede. Um endereço de duas partes, conveniente para especificar um endereço de classe A, coloca a parte inicial no primeiro byte e a parte final nos três bytes mais à direita do endereço de rede. Um endereço de três partes, conveniente para especificar um endereço de classe B, coloca a primeira parte no primeiro byte, a segunda parte no segundo byte e a última parte nos dois bytes mais à direita do endereço de rede. Por exemplo:
Número de partes e exemplo ipString |
Endereço 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 |
Aplica-se a
Parse(ReadOnlySpan<Char>)
Converte um endereço IP representado como um intervalo de caracteres numa IPAddress instância.
public:
static System::Net::IPAddress ^ Parse(ReadOnlySpan<char> ipString);
public static System.Net.IPAddress Parse(ReadOnlySpan<char> ipString);
static member Parse : ReadOnlySpan<char> -> System.Net.IPAddress
Public Shared Function Parse (ipString As ReadOnlySpan(Of Char)) As IPAddress
Parâmetros
- ipStringipSpan
- ReadOnlySpan<Char>
Um intervalo de caracteres que contém um endereço IP em notação pontuada-quad para IPv4 e em notação dois-dois e hexadecimal para IPv6.
Devoluções
O endereço IP convertido.
Exceções
ipString não é um endereço IP válido.