参考http://blog.csdn.net/yuzhongchun/article/details/11990571
using System.Net; namespace Network_01 { public partial class Main : Form { public Main() { InitializeComponent(); } private void btnGetHostName_Click(object sender, EventArgs e) { DebugInfo.Text = Dns.GetHostName();//返回本机名称 } private void btnGetHostEntry_Click(object sender, EventArgs e) { DebugInfo.Text = ""; IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); //返回目标主机实例 foreach (IPAddress ipAddr in ipEntry.AddressList) { if (ipAddr.AddressFamily.ToString() == "InterNetwork") //IP4 InterNetworkV6 IP6 DebugInfo.Text += ipAddr.AddressFamily.ToString()+":" + ipAddr.ToString() + " "; } } private void WebGetHostEntry_Click(object sender, EventArgs e) { DebugInfo.Text = ""; IPHostEntry ipEntry = Dns.GetHostEntry(AddrText.Text.Trim()); foreach (IPAddress ipAddr in ipEntry.AddressList) { if (ipAddr.AddressFamily.ToString() == "InterNetwork") //IP4 DebugInfo.Text += ipAddr.AddressFamily.ToString() + ":" + ipAddr.ToString() + " "; } } } }