I have code, which is using System.Net
and System.Net.NetworkInformation
references, it generates a list of my network connection names.
Everything seems fine and working, but when I made a class of this code, and exported values to listbox1
items add, I had only one network connection name, but really 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;
}
}
Copy and Paste of https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx
I would modify the code you currently have:
To be like this:
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>
andyield return
:Either way, you would use it like this:
(On a side note, I would recommend that you use the .NET Framework Naming Guide)
You only return the last item
value = nic.Name;
You should use an array or List to return all items