c#网络适配器列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11677815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:48:04  来源:igfitidea点击:

c# Network adapters list

c#listnetworking

提问by deleted

I have code, which is using System.Netand System.Net.NetworkInformationreferences, it generates a list of my network connection names.

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

Everything seems fine and working, but when I made a class of this code, and exported values to listbox1items add, I had only one network connection name, but really I have four.

一切看起来都很好并且可以正常工作,但是当我编写了一个此类代码并将值导出到listbox1项目添加时,我只有一个网络连接名称,但实际上我有四个。

How can I solve this problem?

我怎么解决这个问题?

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;
    }
}

采纳答案by Nate

I would modify the code you currently have:

我会修改您目前拥有的代码:

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; 
} 

To be like this:

要像这样:

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

A more fancy way (although it probably doesn't matter because GetAllNetworkIntefaces probably blocks until it has has a full list) would be to use IEnumerable<T>and yield return:

一种更奇特的方式(虽然它可能并不重要,因为 GetAllNetworkIntefaces 可能会阻塞,直到它有一个完整的列表)将使用IEnumerable<T>yield return

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

Either way, you would use it like this:

无论哪种方式,您都会像这样使用它:

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

(On a side note, I would recommend that you use the .NET Framework Naming Guide)

(附带说明,我建议您使用.NET Framework Naming Guide

回答by L.B

You only return the last item value = nic.Name;You should use an array or List to return all items

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

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

回答by Yash

Copy and Paste of https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

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

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();
}