在win form程序中获取本机的IP地址
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace testLocalIP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string hostname;
System.Net.IPHostEntry localhost;
System.Net.IPAddress localaddr;
hostname = System.Net.Dns.GetHostName();
localhost = System.Net.Dns.GetHostByName(hostname);
localaddr = localhost.AddressList[0]; //localaddr中就是本机ip地址
this.textBox1.Text = localhost.AddressList[0].ToString();
this.textBox2.Text = System.Net.Dns.GetHostName().ToString();
this.textBox3.Text = System.Net.Dns.GetHostByName(hostname).ToString();
}
}
}