GetHostByAddress(IPAddress address):依 IPAddress 物件取得 IPHostEntry。GetHostByAddress(string address):依 IP 字串取得 IPHostEntry。GetHostName():取得本機 DNS 主機名。
Resolve(string hostNameOrAddress):解析主機名或 IP,回傳 IPHostEntry(含位址清單)。
// 取得主機名
string host = Dns.GetHostName();
IPHostEntry entry = Dns.Resolve(host);
textBox1.Text = host;
// 依 IP 或電腦名解析主機名
IPHostEntry info = Dns.Resolve(textBox1.Text.Trim());
MessageBox.Show(info.HostName);
// 取得 IP 清單
IPHostEntry ipEntry = Dns.GetHostByName(textBox1.Text);
foreach (IPAddress ip in ipEntry.AddressList)
MessageBox.Show(ip.ToString());
適用於網路診斷工具、伺服器探測等場景。