C#编程获取IP地址、子网掩码、网关地址

来源:互联网 发布:js重新加载指定div 编辑:程序博客网 时间:2024/04/28 14:42

C#编程获取IP地址、子网掩码、网关地址

 

添加对程序集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]);
                }
            }
        }
    }
}

原创粉丝点击