C#网络适配器列表 [英] c# Network adapters list

查看:352
本文介绍了C#网络适配器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一切似乎罚款和工作,但当我做一个类的代码,并出口值ListBox1中添加项目,我只有一个网络连接的名称,但真的我有四个。



我怎样才能解决这一问题。

 私人无效的button1_Click(对象发件人,EventArgs五)
{
适配器的obj =新适配器();
VAR值= obj.net_adapters();
listBox1.Items.Add(值);
}

公共类适配器
{

公共字符串net_adapters()
{
字符串值=的String.Empty;
的foreach(NetworkInterface的NIC在NetworkInterface.GetAllNetworkInterfaces())
{
值= nic.Name;
}
返回值;
}
}


解决方案

我将修改当前的代码:

 公共字符串net_adapters()
{
字符串值=字符串.Empty;
的foreach(NetworkInterface的NIC在NetworkInterface.GetAllNetworkInterfaces())
{
//错误在你原来的代码在这里是'='
//你proably打算做类似值+ =,+ nic.Name
//这不会与列表框项目集合
值= nic.Name很好地工作;
}
返回值;
}



是这样的:

 公共System.Collections.Generic.List<串GT; net_adapters()
{
名单,LT;弦乐>值=新的List<串GT;();
的foreach(NetworkInterface的NIC在NetworkInterface.GetAllNetworkInterfaces())
{
values​​.Add(nic.Name);
}
返回值;
}

一个更奇特的方式(虽然它可能并不重要,因为GetAllNetworkIntefaces可能块直到它有一个完整的清单)是使用的IEnumerable< T> 收益回报率

 公开的IEnumerable<串GT; net_adapters()
{
的foreach(NetworkInterface的NIC在NetworkInterface.GetAllNetworkInterfaces())
{
收益率的回报nic.Name;
}
产量突破;
}



无论哪种方式,你会使用这样的:

  VAR OBJ =新的适配器(); 
变种值= obj.net_adapters();
listBox1.ItemsSource =值;



(在一个侧面说明,我会建议你使用的.NET框架命名指南


I have code, which using System.Net and System.Net.NetworkInformation references , generate list of my network connection names.

Everything seems fine and working, but when I make a class of this code, and export values to listbox1 items add, I had only one network connection name, but realy 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:

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:

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)

这篇关于C#网络适配器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆