public List<string> ipList { get; set; }
private void GetIP()
{
ipList = new List<string>();
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期, //我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
ipList.Add(ip.ToString());
}
if (ipList.Count > 0)
{
ipComboBox.SelectedIndex = 0;
}
}