Csharp/C#教程:c#网络适配器列表分享


c#网络适配器列表

我有代码,它使用System.NetSystem.Net.NetworkInformation引用,它生成一个我的网络连接名称列表。

一切似乎都很好并且正常工作,但是当我创建一个这样的代码,并将导出的值导出到listbox1项目添加时,我只有一个网络连接名称,但实际上我有四个。

我怎么解决这个问题?

 private void button1_Click(object sender, EventArgs e) { Adapters obj = new Adapters(); var value = obj.net_adapters(); listBox1.Items.Add(value); } public class Adapters { public string net_adapters() { string value = string.Empty; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { value = nic.Name; } return value; } } 

我会修改你目前的代码:

 public string net_adapters() { string value = string.Empty; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { // bug in your original code right here is `=` // you proably meant to do something like value += ", " + nic.Name // which would not work well with listbox Items collection value = nic.Name; } return value; } 

是这样的:

 public System.Collections.Generic.List net_adapters() { List values = new List(); foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { values.Add(nic.Name); } return values; } 

一种更奇特的方式(虽然它可能无关紧要,因为GetAllNetworkIntefaces可能阻塞,直到它有一个完整列表)将使用IEnumerableyield return

 public IEnumerable net_adapters() { foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { yield return nic.Name; } yield break; } 

不管怎样,你会像这样使用它:

 var obj = new Adapters(); var values = obj.net_adapters(); listBox1.ItemsSource = values; 

(另外,我建议您使用.NET Framework命名指南 )

你只返回最后一项value = nic.Name; 您应该使用数组或列表来返回所有项目

 public List net_adapters() { List values = new List(); foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { values.Add(nic.Name); } return values; } 

复制和粘贴https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

上述就是C#学习教程:c#网络适配器列表分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public static void ShowNetworkInterfaces() { IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); Console.WriteLine("Interface information for {0}.{1} ", computerProperties.HostName, computerProperties.DomainName); if (nics == null || nics.Length < 1) { Console.WriteLine(" No network interfaces found."); return; } Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length); foreach (NetworkInterface adapter in nics) { IPInterfaceProperties properties = adapter.GetIPProperties(); Console.WriteLine(); Console.WriteLine(adapter.Description); Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'=')); Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType); Console.WriteLine(" Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString()); Console.WriteLine(" Operational status ...................... : {0}", adapter.OperationalStatus); string versions =""; // Create a display string for the supported IP versions. if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { versions = "IPv4"; } if (adapter.Supports(NetworkInterfaceComponent.IPv6)) { if (versions.Length > 0) { versions += " "; } versions += "IPv6"; } Console.WriteLine(" IP version .............................. : {0}", versions); ShowIPAddresses(properties); // The following information is not useful for loopback adapters. if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback) { continue; } Console.WriteLine(" DNS suffix .............................. : {0}", properties.DnsSuffix); string label; if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties(); Console.WriteLine(" MTU...................................... : {0}", ipv4.Mtu); if (ipv4.UsesWins) { IPAddressCollection winsServers = properties.WinsServersAddresses; if (winsServers.Count > 0) { label = " WINS Servers ............................ :"; ShowIPAddresses(label, winsServers); } } } Console.WriteLine(" DNS enabled ............................. : {0}", properties.IsDnsEnabled); Console.WriteLine(" Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled); Console.WriteLine(" Receive Only ............................ : {0}", adapter.IsReceiveOnly); Console.WriteLine(" Multicast ............................... : {0}", adapter.SupportsMulticast); ShowInterfaceStatistics(adapter); Console.WriteLine(); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1003580.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐