Dns.Resolve(String) 方法

定義

警告

Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202

警告

Resolve has been deprecated. Use GetHostEntry instead.

警告

Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202

解析 DNS 主機名稱或 IP 位址至 IPHostEntry 執行個體。

public:
 static System::Net::IPHostEntry ^ Resolve(System::String ^ hostName);
[System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry Resolve (string hostName);
[System.Obsolete("Resolve has been deprecated. Use GetHostEntry instead.")]
public static System.Net.IPHostEntry Resolve (string hostName);
[System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry Resolve (string hostName);
public static System.Net.IPHostEntry Resolve (string hostName);
[<System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")>]
static member Resolve : string -> System.Net.IPHostEntry
[<System.Obsolete("Resolve has been deprecated. Use GetHostEntry instead.")>]
static member Resolve : string -> System.Net.IPHostEntry
[<System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
static member Resolve : string -> System.Net.IPHostEntry
static member Resolve : string -> System.Net.IPHostEntry
Public Shared Function Resolve (hostName As String) As IPHostEntry

參數

hostName
String

DNS 式網域名稱或 IP 位址。

傳回

IPHostEntry 執行個體,含有在 hostName 中所指定之主機的相關位址資訊。

屬性

例外狀況

hostNamenull

hostName 的長度大於 255 個字元。

當解析 hostName 時,發生錯誤。

範例

下列範例會 Resolve 使用 方法將IP位址解析為 IPHostEntry 實例。

try
{
   IPHostEntry^ hostInfo = Dns::Resolve( hostString );
   
   // Get the IP address list that resolves to the host names contained in the
   // Alias property.
   array<IPAddress^>^address = hostInfo->AddressList;
   
   // Get the alias names of the addresses in the IP address list.
   array<String^>^alias = hostInfo->Aliases;
   Console::WriteLine( "Host name : {0}", hostInfo->HostName );
   Console::WriteLine( "\nAliases : " );
   for ( int index = 0; index < alias->Length; index++ )
   {
      Console::WriteLine( alias[ index ] );

   }
   Console::WriteLine( "\nIP Address list :" );
   for ( int index = 0; index < address->Length; index++ )
   {
      Console::WriteLine( address[ index ] );

   }
}
catch ( SocketException^ e ) 
{
   Console::WriteLine( "SocketException caught!!!" );
   Console::WriteLine( "Source : {0}", e->Source );
   Console::WriteLine( "Message : {0}", e->Message );
}
catch ( ArgumentNullException^ e ) 
{
   Console::WriteLine( "ArgumentNullException caught!!!" );
   Console::WriteLine( "Source : {0}", e->Source );
   Console::WriteLine( "Message : {0}", e->Message );
}
catch ( NullReferenceException^ e ) 
{
   Console::WriteLine( "NullReferenceException 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 );
}
try {
    IPHostEntry hostInfo = Dns.Resolve(hostString);
    // Get the IP address list that resolves to the host names contained in the
    // Alias property.
    IPAddress[] address = hostInfo.AddressList;
    // Get the alias names of the addresses in the IP address list.
    String[] alias = hostInfo.Aliases;

    Console.WriteLine("Host name : " + hostInfo.HostName);
    Console.WriteLine("\nAliases : ");
    for(int index=0; index < alias.Length; index++) {
      Console.WriteLine(alias[index]);
    }
    Console.WriteLine("\nIP Address list :");
    for(int index=0; index < address.Length; index++) {
       Console.WriteLine(address[index]);
    }
 }
 catch(SocketException e)
 {
    Console.WriteLine("SocketException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(ArgumentNullException e)
 {
Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(NullReferenceException e)
 {
     Console.WriteLine("NullReferenceException 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);
 }
Try
    ' Call the Resolve method passing a DNS style host name or an IP address in 
    ' dotted-quad notation (for example, "www.contoso.com" or "207.46.131.199") to 
    ' obtain an IPHostEntry instance that contains address information for the 
    ' specified host.
    Dim hostInfo As IPHostEntry = Dns.Resolve(hostString)
    ' Get the IP address list that resolves to the host names contained in the Alias 
    ' property.
    Dim address As IPAddress() = hostInfo.AddressList
    ' Get the alias names of the addresses in the IP address list.
    Dim [alias] As [String]() = hostInfo.Aliases

    Console.WriteLine(("Host name : " + hostInfo.HostName))
    Console.WriteLine(ControlChars.Cr + "Aliases : ")
    Dim index As Integer
    For index = 0 To [alias].Length - 1
        Console.WriteLine([alias](index))
    Next index
    Console.WriteLine(ControlChars.Cr + "IP Address list :")

    For index = 0 To address.Length - 1
        Console.WriteLine(address(index))
    Next index
Catch e As SocketException
    Console.WriteLine("SocketException caught!!!")
    Console.WriteLine(("Source : " + e.Source))
    Console.WriteLine(("Message : " + e.Message))
Catch e As ArgumentNullException
    Console.WriteLine("ArgumentNullException caught!!!")
    Console.WriteLine(("Source : " + e.Source))
    Console.WriteLine(("Message : " + e.Message))
Catch e As NullReferenceException
    Console.WriteLine("NullReferenceException 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

備註

方法 Resolve 會查詢 DNS 伺服器,以取得與主機名或 IP 位址相關聯的 IP 位址。

hostName 是與多個IP位址相關聯的 DNS 樣式主機名時,只會傳回解析為該主機名的第一個IP位址。

Ipv6Element.Enabled如果 屬性設定為 true,則Aliases傳回之實例的 IPHostEntry 屬性不會由這個方法填入,而且一律為空白。

注意

當您在應用程式中啟用網路追蹤時,此成員會發出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於