IPAddress.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| Parse(String) |
将 IP 地址字符串转换为 IPAddress 实例。 |
| Parse(ReadOnlySpan<Char>) |
将表示为字符范围的 IP 地址转换为 IPAddress 实例。 |
Parse(String)
将 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 的点象表示法和 IPv6 的冒号十六进制表示法表示的 IP 地址创建 IPAddress 实例。
确定 IP 地址的构造方式时(每个部分都用句点分隔) ipString 的数目。 一部分地址直接存储在网络地址中。 一个两部分地址,方便指定类 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<Char>)
将表示为字符范围的 IP 地址转换为 IPAddress 实例。
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
参数
- ipStringipSpan
- ReadOnlySpan<Char>
包含 IPv4 的点象表示法和 IPv6 的冒号十六进制表示法中的 IP 地址的字符范围。
返回
转换后的 IP 地址。
例外
ipString 不是有效的 IP 地址。