C# 獲取本機的IP地址
控制檯命令程式:
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問題,