遍历主机所有的IP地址放入组件ComboBox
private void Form1_Load(object sender, EventArgs e)
{
iplist.Items.Add("请选择IP地址");
List<string> str = new List<string>();
string hostname = Dns.GetHostName();
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostname);
foreach (IPAddress ip in addressList)
{
string _temp_ip = ip.MapToIPv4().ToString();
string[] ipstr = _temp_ip.Split('.');
if (ipstr[0] == "127" || ipstr[0] == "10" || ipstr[0] == "172")
{
continue;
}
else
{
string temp = str.Where(s => s == _temp_ip).FirstOrDefault();
if (temp == null)
{
str.Add(_temp_ip);
iplist.Items.Add(_temp_ip);
}
}
}
iplist.SelectedIndex = 0;
}
