語言

IPAddress.Parse 方法

定義

多載

名稱 Description
Parse(ReadOnlySpan<Char>)

將以字元區段表示的 IP 位址轉換為實 IPAddress 例。

Parse(String)

將 IP 位址字串轉換成實 IPAddress 例。

Parse(ReadOnlySpan<Byte>)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>)

來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs

將以字元區段表示的 IP 位址轉換為實 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

參數

ipStringipSpan
ReadOnlySpan<Char>

一個字元區間,IPv4 以虛線四邊形表示 IP 位址,IPv6 則以冒號十六進位表示。

傳回

轉換後的 IP 位址。

例外狀況

ipString 不是有效的 IP 位址。

適用於

Parse(String)

來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs
來源:
IPAddress.cs

將 IP 位址字串轉換成實 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

參數

ipString
String

一個包含 IPv4 點四進位符號 IP 位址,IPv6 則以冒號十六進位表示的字串。

傳回

一個 IPAddress 例子。

例外狀況

ipString 是 null。

ipString 不是有效的 IP 位址。

範例

以下程式碼將包含 IP 位址的字串(IPv4 為虛點四分表示,IPv6 為冒十六進制)轉換為該 IPAddress 類別的實例。 接著使用過載 ToString 方法以標準符號顯示地址。


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

備註

靜態 Parse 方法從 IPv4 以虛點四邊形表示的 IP 位址建立 IPAddress 實例,IPv6 則以冒號十六進位表示。

其中 ipString 的部分數量(每個部分以一個句點分隔)決定了 IP 位址的構造方式。 一個單一部分的位址直接儲存在網路位址中。 兩部分位址方便指定類別 A 位址,將首部分置於第一個位元組,後置部分置於網路位址最右側的三個位元組。 一個方便指定類別 B 位址的三部分位址,將第一部分放入第一個位元組,第二部分置於第二個位元組,最後一個部分置於網路位址最右邊的兩個位元組。 例如:

零件數量與範例 ipString IPAddress 的 IPv4 位址
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

適用於

Parse(ReadOnlySpan<Byte>)

來源:
IPAddress.cs
來源:
IPAddress.cs

將UTF-8字元的範圍剖析為值。

public:
 static System::Net::IPAddress ^ Parse(ReadOnlySpan<System::Byte> utf8Text);
public static System.Net.IPAddress Parse(ReadOnlySpan<byte> utf8Text);
static member Parse : ReadOnlySpan<byte> -> System.Net.IPAddress
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte)) As IPAddress

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

傳回

解析 的結果 utf8Text。

適用於