首先找到资源管理器里的引用,右击添加引用:System.Management;
- using System;
- using System.Management;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection nics = mc.GetInstances();
- foreach (ManagementObject nic in nics)
- {
- if (Convert.ToBoolean(nic["ipEnabled"]) == true)
- {
- Console.WriteLine((nic["IPAddress"] as String[])[0]);
- Console.WriteLine((nic["IPSubnet"] as String[])[0]);
- Console.WriteLine((nic["DefaultIPGateway"] as String[])[0]);
- }
- }
- }
- }
- }