Dzwebs.Net

撰写电脑技术杂文十余年

多网卡多版本IP如何筛选出C类地址

Admin | 2014-12-20 9:46:58 | 被阅次数 | 4915

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  比如说,有多个网卡,并且,分别配置的是IPV4或IPV6版本的IP地址,那么,我们如何通过C#代码获取C类的IP地址呢,比如192.168.0.123……

  下面是代码:已测试成功!

  ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = wmi.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                string tmp = mo["Description"].ToString();             
                if (tmp.Contains("PCI") && !tmp.Contains("Wireless") && (bool)mo["IPEnabled"] == true)
                {                                                       
                        IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());

      string[] ipaddress=(System.String[])mo["IPAddress"];
                        foreach (IPAddress ip in arrIPAddresses)
                        {
                            if (ip.AddressFamily == AddressFamily.InterNetwork)
                            {                         
                                Console.WriteLine(ip.ToString());                               
                            }
                        }                   
                }
            }


网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231