本地以太网IP 192.168.0.106例如。我的代码检索适配器的信息序列是无线局域网,本地以太网,但IP序列是192.168.0.102,192.168.0.106。但这与实际情况不符。 是否可以支持我这个。In my application programe, I need get the network adapter information to the user selection.following is my code, the issue is if network enviornment is Wireless LAN IP 192.168.0.102,Local Ethernet IP 192.168.0.106 for example. my code retrieve the information sequence for adapter is Wireless LAN, Local Ethernet, but for the IP sequence is 192.168.0.102, 192.168.0.106. but that is not match the real situation.whether you can support me about this.foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()){ if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { if (nic.OperationalStatus == OperationalStatus.Up) Adapter_LstB.Items.Add(nic.Name); } if (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { if (nic.OperationalStatus == OperationalStatus.Up) Adapter_LstB.Items.Add(nic.Name); }}foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName())){ if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString()) IPAddress_LstB.Items.Add(currrentIPAddress.ToString());}ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection moc = mc.GetInstances();foreach (ManagementObject mo in moc){ if (mo["IPEnabled"].ToString() == "True") MACAddress_LstB.Items.Add(mo["MacAddress"].ToString());}HostPC_IP.Text = IPAddress_LstB.Items[0].ToString();推荐答案自己解决 ManagementClass mc = new ManagementClass(Win32_NetworkAdapterConfiguration); ManagementObjectCollection moc = mc.GetInstances(); foreach(管理对象mo in moc) { if (mo [IPEnabled]。ToString()==True) { Adapter_LstB.Items.Add(mo [Caption]。ToString() .Substring(11)); string [] ipaddresses =(string [])mo [IPAddress]; IPAddress_LstB.Items.Add(ipaddresses [0] ); MACAddress_LstB.Items.Add(mo [MacAddress]。ToString()); } }Solve by myself ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["IPEnabled"].ToString() == "True") { Adapter_LstB.Items.Add(mo["Caption"].ToString().Substring(11)); string[] ipaddresses = (string[])mo["IPAddress"]; IPAddress_LstB.Items.Add(ipaddresses[0]); MACAddress_LstB.Items.Add(mo["MacAddress"].ToString()); } } 这篇关于C#获取网络适配器和IP / MAC地址,但检索信息问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 21:54