如何获取多个网络接口卡数据

时间:2012-07-28 17:10:49

标签: c# nic

我有代码,可以找到我的网卡IP地址,IP子网,网关,Mac和网卡名称(说明)。但问题是,我的PC上有多个NIC卡和WiFi。而这个程序而不是4个NIC卡显示我只有一个主要卡。 怎么能解决这个问题呢?

public  void NIC_data()
    {
        ManagementObjectSearcher query = new
        ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
        ManagementObjectCollection queryCollection = query.Get();
        foreach (ManagementObject mo in queryCollection)
        {
            string[] addresses = (string[])mo["IPAddress"];
            string[] subnets = (string[])mo["IPSubnet"];
            string[] defaultgateways = (string[])mo["DefaultIPGateway"];
            textBox1.Text = string.Format("Network Card: {0}", mo["Description"]);
            textBox2.Text = string.Format(" MAC Address: {0}", mo["MACAddress"]);
            foreach (string ipaddress in addresses)
            {
                textBox3.Text = string.Format(" IP Address: {0}", ipaddress);
            }
            foreach (string subnet in subnets)
            {
                textBox4.Text = string.Format(" Subnet Mask: {0}", subnet);
            }
            foreach (string defaultgateway in defaultgateways)
            {
                textBox5.Text = string.Format(" Gateway: {0}", defaultgateway);
            }
        }

1 个答案:

答案 0 :(得分:3)

您只需将循环的最后一个值分配给textBoxes,如textBox3.Text = ....

要么附加到textBox3.Text += ...,要么使用组合框。