添加对程序集System.Management的引用

  1. using System;
  2. using System.Management;
  3. namespace ConsoleApplication1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  10.             ManagementObjectCollection nics = mc.GetInstances();
  11.             foreach (ManagementObject nic in nics)
  12.             {
  13.                 if (Convert.ToBoolean(nic["ipEnabled"]) == true)
  14.                 {
  15.                     Console.WriteLine((nic["IPAddress"] as String[])[0]);
  16.                     Console.WriteLine((nic["IPSubnet"] as String[])[0]);
  17.                     Console.WriteLine((nic["DefaultIPGateway"] as String[])[0]);
  18.                 }
  19.             }
  20.         }
  21.     }
  22. }