IPAddress.TryParse 方法

定義

多載

TryParse(String, IPAddress)

判斷字串是否為有效的 IP 位址。Determines whether a string is a valid IP address.

TryParse(ReadOnlySpan<Char>, IPAddress)

判斷所指定位元組範圍是否代表有效的 IP 位址。Determines whether the specified byte span represents a valid IP address.

TryParse(String, IPAddress)

判斷字串是否為有效的 IP 位址。Determines whether a string is a valid IP address.

public:
 static bool TryParse(System::String ^ ipString, [Runtime::InteropServices::Out] System::Net::IPAddress ^ % address);
public static bool TryParse (string ipString, out System.Net.IPAddress address);
static member TryParse : string * IPAddress -> bool
Public Shared Function TryParse (ipString As String, ByRef address As IPAddress) As Boolean

參數

ipString
String

要驗證的字串。The string to validate.

address
IPAddress

字串的 IPAddress 版本。The IPAddress version of the string.

傳回

如果 ipString 可以剖析為 IP 位址,則為 true;否則為 falsetrue if ipString was able to be parsed as an IP address; otherwise, false.

例外狀況

ipString 為 null。ipString is null.

備註

請注意,這個方法會接受有效的 ipString 值,可剖析為 Int64,然後將該 Int64 視為網路位元組順序中 IP 位址的完整值,類似于 IPAddress 的「函式」執行的方式。Note that this method accepts as valid an ipString value that can be parsed as an Int64, and then treats that Int64 as the long value of an IP address in network byte order, similar to the way that the IPAddress constructor does. 這表示如果 Int64 已成功剖析,則此方法會傳回 true,即使它代表的位址不是有效的 IP 位址。This means that this method returns true if the Int64 is parsed successfully, even if it represents an address that's not a valid IP address. 例如,如果 ipString 是 "1",則即使 "1" (或0.0.0.1)不是有效的 IP 位址,這個方法也會傳回 true,而且您可能會預期這個方法傳回 false。For example, if ipString is "1", this method returns true even though "1" (or 0.0.0.1) is not a valid IP address and you might expect this method to return false. 修正此 bug 會中斷現有的應用程式,因此不會變更目前的行為。Fixing this bug would break existing apps, so the current behavior will not be changed. 您的程式碼可以藉由確保它只使用這個方法來剖析以點十進位格式的 IP 位址,以避免此行為。Your code can avoid this behavior by ensuring that it only uses this method to parse IP addresses in dotted-decimal format.

TryParse(ReadOnlySpan<Char>, IPAddress)

判斷所指定位元組範圍是否代表有效的 IP 位址。Determines whether the specified byte span represents a valid IP address.

public:
 static bool TryParse(ReadOnlySpan<char> ipString, [Runtime::InteropServices::Out] System::Net::IPAddress ^ % address);
public static bool TryParse (ReadOnlySpan<char> ipString, out System.Net.IPAddress address);
static member TryParse : ReadOnlySpan<char> * IPAddress -> bool
Public Shared Function TryParse (ipString As ReadOnlySpan(Of Char), ByRef address As IPAddress) As Boolean

參數

ipString
ReadOnlySpan<Char>

要驗證的位元組範圍。The byte span to validate.

address
IPAddress

當此方法傳回時,為位元組範圍的 IPAddress 版本。When this method returns, the IPAddress version of the byte span.

傳回

如果 ipString 可以剖析為 IP 位址,則為 true;否則為 falsetrue if ipString was able to be parsed as an IP address; otherwise, false.

適用於