0

我有代码,可以找到我的 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);
            }
        }
4

1 回答 1

3

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

附加到textBox3.Text += ...或使用组合框。

于 2012-07-28T17:34:14.313 回答