控制台命令程序:
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine(ipa.ToString());
}
Console.ReadLine();
窗体:
private void button1_Click(object sender, EventArgs e)
{
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
textBox1.Text = textBox1.Text + ipa.ToString()+"\t";
}
}
}
直接复制的,请 注意使用窗体的控件name问题,
以上内容,部分来自于:https://www.cnblogs.com/iack/p/3685680.html