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