IPAddress.Parse 方法

定義

多載

Parse(ReadOnlySpan<Char>)

將以字元範圍表示的 IP 位址轉換為 IPAddress 執行個體。Converts an IP address represented as a character span to an IPAddress instance.

Parse(String)

將 IP 位址字串轉換為 IPAddress 執行個體。Converts an IP address string to an IPAddress instance.

Parse(ReadOnlySpan<Char>)

將以字元範圍表示的 IP 位址轉換為 IPAddress 執行個體。Converts an IP address represented as a character span to an IPAddress instance.

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

參數

ipString
ReadOnlySpan<Char>

字元範圍,包含使用 IPv4 點分隔四組數字標記法和 IPv6 冒號分隔十六進位標記法的 IP 位址。A character span that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

傳回

轉換的 IP 位址。The converted IP address.

例外狀況

ipString 不是有效的 IP 位址。ipString is not a valid IP address.

Parse(String)

將 IP 位址字串轉換為 IPAddress 執行個體。Converts an IP address string to an IPAddress instance.

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 點分隔四組數字標記法和 IPv6 冒號分隔十六進位標記法的 IP 位址。A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

傳回

IPAddress 執行個體。An IPAddress instance.

例外狀況

ipStringnullipString is null.

ipString 不是有效的 IP 位址。ipString is not a valid IP address.

範例

下列程式碼會將包含 IP 位址的字串(以 IPv4 的點四標記法或 IPv6 的冒號十六進位標記法)轉換成 IPAddress 類別的實例。The following code converts a string that contains an IP address, in dotted-quad notation for IPv4 or in colon-hexadecimal notation for IPv6, into an instance of the IPAddress class. 然後,它會使用多載的 ToString 方法,以標準標記法顯示位址。Then it uses the overloaded ToString method to display the address in standard notation.

#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

備註

靜態 Parse 方法會從 IP 位址建立 IPAddress 實例,其以以點四標記法表示的 IPv4,並以冒號-十六進位標記法用於 IPv6。The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

ipString 中的元件數目(每個部分以句點分隔)會決定 IP 位址的結構。The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. 一個部分位址會直接儲存在網路位址中。A one part address is stored directly in the network address. 有兩個部分的位址,方便您指定類別的位址,將前置部分放在第一個位元組,並將尾端部分放置在最右邊三個位元組的網路位址中。A two part address, convenient for specifying a class A address, puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. 有三個部分的位址,方便您指定類別 B 位址,將第一個部分放在第一個位元組,第二個部分是第二個位元組,而最後一個部分是網路位址最右邊的兩個位元組。A three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part in the second byte, and the final part in the right-most two bytes of the network address. 例如,For example:

元件數目和範例 ipStringNumber of parts and example ipString IPAddress 的 IPv4 位址IPv4 address for IPAddress
1--"65535"1 -- "65535" 0.0.255.2550.0.255.255
2 -- "20.2"2 -- "20.2" 20.0.0.220.0.0.2
2 -- "20.65535"2 -- "20.65535" 20.0.255.25520.0.255.255
3 -- "128.1.2"3 -- "128.1.2" 128.1.0.2128.1.0.2

適用於