获取当前主机IPV4
gaoqiqi888吧
全部回复
仅看楼主
level 9
gaoqiqi888 楼主
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox_ip.DataSource = getIPv4List();
comboBox_ipv2.DataSource = getIPv4List_v2();
}
private List<IPAddress> getIPv4List()
{
List<IPAddress> ipaddlist = new List<IPAddress>();
string hostName = Dns.GetHostName();
IPHostEntry _ipHostEntry = Dns.GetHostEntry(hostName);
IPAddress[] _ipaddressArray = _ipHostEntry.AddressList;
foreach (IPAddress _ipaddress in _ipaddressArray)
{
if (_ipaddress.ToString().Length >= 7 && _ipaddress.ToString().Length <= 15)
{
ipaddlist.Add(_ipaddress);
}
}
return ipaddlist;
}
private List<IPAddress> getIPv4List_v2()
{
string hostName = Dns.GetHostName();
IPAddress[] _ipaddressArray = Dns.GetHostAddresses(hostName);
List<IPAddress> _ipok = new List<IPAddress>();
foreach (IPAddress ip in _ipaddressArray)
{
if (ip.ToString().Contains('.'))
{
_ipok.Add(ip);
}
}
return _ipok;
}
}
}
2020年04月06日 03点04分 1
1