根据DNS获取IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public static List<string> ipaddressList() { IPAddress[] AddressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; List<string> iplist = new List<string>(); for (int i = 0; i < AddressList.Length; i++) { IPAddress _IPAddress = AddressList[i];
if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { iplist.Add(_IPAddress.ToString()); } } return iplist; }
public static string ipaddress() { string _ipaddress = null; List<string> iplist = ipaddressList();
for (int i = 0; i < iplist.Count; i++) { var ipaddress = iplist[i]; if ( ipaddress.StartsWith("192.168.1.") || ipaddress.StartsWith("192.168.2.") || ipaddress.StartsWith("192.168.3.") || ipaddress.StartsWith("192.168.10.")) { _ipaddress = ipaddress; return _ipaddress; } } for (int i = 0; i < iplist.Count; i++) { var ipaddress = iplist[i]; if (ipaddress.StartsWith("192")) { _ipaddress = ipaddress; return _ipaddress; } } if (_ipaddress == null && iplist.Count > 0) { _ipaddress = iplist[0]; } return _ipaddress; }
|
注意
我们获取到的IP会是多个,这种方法无法获取虚拟网卡
根据网络设备获取IP
获取所有激活设备的IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static void GetIP() { NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); int len = interfaces.Length;
for (int i = 0; i < len; i++) { NetworkInterface ni = interfaces[i]; if (ni.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties property = ni.GetIPProperties(); foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { string address = ip.Address.ToString(); string niname = ni.Name.ToString(); Console.WriteLine("【" + niname + "】:" + address); } } } } }
|
结果:
【以太网】:192.168.2.218
【VMware Network Adapter VMnet1】:192.168.211.1
【VMware Network Adapter VMnet8】:192.168.7.1
【adt_tap_dvr_for_sc】:10.88.19.16
【WLAN】:192.168.3.99
【Loopback Pseudo-Interface 1】:127.0.0.1
如果不添加ni.OperationalStatus == OperationalStatus.Up,获取到的结果如下:
【以太网 2】:2.0.1.5
【以太网】:192.168.2.218
【本地连接 1】:169.254.229.179
【本地连接 2】:169.254.68.218
【VMware Network Adapter VMnet1】:192.168.211.1
【VMware Network Adapter VMnet8】:192.168.7.1
【adt_tap_dvr_for_sc】:10.88.19.16
【WLAN】:192.168.3.99
【蓝牙网络连接】:169.254.83.215
【Loopback Pseudo-Interface 1】:127.0.0.1
只获取以太网和无线
稍微封装一下方便下拉菜单调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public static DataTable GetIPList() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Key", typeof(string))); dt.Columns.Add(new DataColumn("Value", typeof(string)));
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); int len = interfaces.Length;
for (int i = 0; i < len; i++) { NetworkInterface ni = interfaces[i]; string niname = ni.Name.ToString().ToUpper();
if (ni.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties property = ni.GetIPProperties(); if ( ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ( niname.StartsWith("以太网") || niname.StartsWith("ETH") || niname.StartsWith("本地") ) ) { foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { string address = ip.Address.ToString(); dt.Rows.Add(address, niname); } } } } } return dt; }
|
其中:
获取到的设备类型的枚举
NetworkInterfaceType
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.networkinformation.networkinterfacetype?view=netframework-4.7.2
其中
WLAN是 NetworkInterfaceType.Wireless80211
而有线可能是Ethernet、Ethernet3Megabit或FastEthernetFx
OperationalStatus
| 类型 |
值 |
说明 |
| Dormant |
5 |
网络接口不处于传输数据包的状态;它正等待外部事件。 |
| Down |
2 |
网络接口无法传输数据包。 |
| LowerLayerDown |
7 |
网络接口无法传输数据包,因为它运行在一个或多个其他接口之上,而这些“低层”接口中至少有一个已关闭。 |
| NotPresent |
6 |
由于缺少组件(通常为硬件组件),网络接口无法传输数据包。 |
| Testing |
3 |
网络接口正在运行测试。 |
| Unknown |
4 |
网络接口的状态未知。 |
| Up |
1 |
网络接口已运行,可以传输数据包。 |
ComboBox中显示
下拉菜单
1 2 3 4 5 6 7 8 9 10 11 12 13
| <ComboBox x:Name="ip_cb" Height="30" MinWidth="160" Margin="0,0,0,0" Padding="10,0,10,0" VerticalContentAlignment="Center" Cursor="Hand" DisplayMemberPath="Value" FontSize="14" SelectedValuePath="Key" SelectionChanged="ip_cb_SelectionChanged" Style="{StaticResource ComboBoxStyle}" />
|
回调
1 2 3 4 5 6 7
| private void ip_cb_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { if (ip_cb.SelectedValue != null) { ZConfig.localIP = ip_cb.SelectedValue.ToString(); } }
|
初始化值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| DataTable dt = ZConfig.GetIPList(); ip_cb.ItemsSource = dt.DefaultView; bool isfind = false; if (ZConfig.localIP != "") { for (int i = 0; i < dt.Rows.Count; i++) { var row = dt.Rows[i]; if (row["Key"].ToString() == ZConfig.localIP) { ip_cb.SelectedIndex = i; isfind = true; } } }
if (!isfind) { if (dt.Rows.Count > 0) { ip_cb.SelectedIndex = 0; } }
|