获取本地 有线 正在使用的网卡信息

获取本地 有线 正在使用的网卡信息

包括 IP 掩码 网关 DNS服务地址 网卡地址 网卡名称等信息

代码如下

C# Code:

using System.Management;



        private void GetCurrentInfo()

        {

            ManagementObjectSearcher query = new

            ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");

            ManagementObjectCollection queryCollection = query.Get();

            try

            {

                foreach (ManagementObject mo in queryCollection)

                {

                    string[] addresses = (string[])mo["IPAddress"];

                    string[] subnets = (string[])mo["IPSubnet"];

                    string[] defaultgateways = (string[])mo["DefaultIPGateway"];

                    lblMacInfo.Text = "网卡: " + mo["Description"];



                    macAddress = (string)mo["MACAddress"];

                    txtBoard.Text = "网卡地址: \r\n" + macAddress + "\r\n";

                    ipAddress = addresses[0];

                    txtBoard.Text += "网络地址: \r\n" + ipAddress + "\r\n";

                    ipSubnet = subnets[0];

                    txtBoard.Text += "子网掩码: \r\n" + ipSubnet + "\r\n";

                    defaultGateway = defaultgateways[0];

                    txtBoard.Text += "默认网关: \r\n" + defaultGateway + "\r\n";

                    dnsServer1 = ((string[])mo["DNSServerSearchOrder"])[0];

                    txtBoard.Text += "主DNS服务:\r\n" + dnsServer1 + "\r\n";

                    dnsServer2 = ((string[])mo["DNSServerSearchOrder"])[1];

                    txtBoard.Text += "备DNS服务:\r\n" + dnsServer2;

                    break;

                }

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

你可能感兴趣的:(获取)