IPAddress.Parse Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Overloads
| Name | Description |
|---|---|
| Parse(String) |
Converteert een IP-adrestekenreeks naar een IPAddress exemplaar. |
| Parse(ReadOnlySpan<Char>) |
Converteert een IP-adres dat wordt weergegeven als een tekenbereik naar een IPAddress exemplaar. |
Parse(String)
Converteert een IP-adrestekenreeks naar een IPAddress exemplaar.
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
Parameters
- ipString
- String
Een tekenreeks die een IP-adres bevat in gestippelde quad-notatie voor IPv4 en in dubbele hexadecimale notatie voor IPv6.
Retouren
Een IPAddress exemplaar.
Uitzonderingen
ipString is null.
ipString is geen geldig IP-adres.
Voorbeelden
Met de volgende code wordt een tekenreeks geconverteerd die een IP-adres bevat, in gestippelde quad-notatie voor IPv4 of in dubbele hexadecimale notatie voor IPv6, in een exemplaar van de IPAddress klasse. Vervolgens wordt de overbelaste ToString methode gebruikt om het adres weer te geven in standaard notatie.
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
Opmerkingen
Met de statische Parse methode wordt een IPAddress exemplaar gemaakt van een IP-adres uitgedrukt in gestippelde-quad-notatie voor IPv4 en in dubbele hexadecimale notatie voor IPv6.
Het aantal onderdelen (elk onderdeel wordt gescheiden door een punt) bepaalt ipString hoe het IP-adres wordt samengesteld. Een deeladres wordt rechtstreeks opgeslagen in het netwerkadres. Een tweedelige adres, handig voor het opgeven van een klasse A-adres, plaatst het voorlooponderdeel in de eerste byte en het volggedeelte in de meest rechtse drie bytes van het netwerkadres. Een driedelig adres, handig voor het opgeven van een klasse B-adres, plaatst het eerste deel in de eerste byte, het tweede deel in de tweede byte en het laatste deel in de meest rechtse twee bytes van het netwerkadres. Voorbeeld:
Aantal onderdelen en voorbeeld ipString |
IPv4-adres voor 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 |
Van toepassing op
Parse(ReadOnlySpan<Char>)
Converteert een IP-adres dat wordt weergegeven als een tekenbereik naar een IPAddress exemplaar.
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
Parameters
- ipStringipSpan
- ReadOnlySpan<Char>
Een tekenbereik dat een IP-adres bevat in gestippelde-quad-notatie voor IPv4 en in dubbele hexadecimale notatie voor IPv6.
Retouren
Het geconverteerde IP-adres.
Uitzonderingen
ipString is geen geldig IP-adres.