區域網路中某電腦有多張網卡 (多個 IP),已知其中一個 IP,欲取得該主機所有解析到的 IP 位址。
string knownIP = "192.168.1.2";
IPHostEntry hostEntry = Dns.GetHostByAddress(knownIP);
foreach (IPAddress ip in hostEntry.AddressList)
{
MessageBox.Show(ip.ToString());
}
Dns.GetHostByAddress 進行反向 DNS 查詢 (PTR 記錄) 取得主機名稱,再解析該主機的所有 A/AAAA 記錄。IPHostEntry.AddressList 包含該主機所有已知 IP。GetHostByAddress 在 .NET Core / .NET 5+ 標記為過時,建議改用 Dns.GetHostEntryAsync。