C # Network Adapters List

I have code that uses links System.Netand System.Net.NetworkInformationit generates a list of names of my network connection.

Everything seems fine and workable, but when I created a class for this code and exported the values ​​to listbox1items add, I only had one network connection name, but actually I have four.

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;
    }
}
+5
source share
3 answers

I would change the code you 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 as follows:

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 bizarre way (although this probably doesn't matter, since GetAllNetworkIntefaces is probably blocked until it has a complete list) will use IEnumerable<T>and yield return:

public IEnumerable<String> 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)

+11

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

Copy and paste 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();
}
0
source

All Articles