當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

C#獲取IP和默認(rèn)網(wǎng)關(guān)
2021-10-21 23:06:29

?

?

        static void Main(string[] args)
        {
            string mac=null,ip=null,ipsubnet=null,ipgateway=null,ipport=null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if ((nic["DefaultIPGateway"] as String[]) != null && Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
                    mac = nic["MacAddress"].ToString();//Mac地址
                    ip = (nic["IPAddress"] as String[])[0];//IP地址
                    ipsubnet = (nic["IPSubnet"] as String[])[0];//子網(wǎng)掩碼
                    ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默認(rèn)網(wǎng)關(guān)
                }
            }
            Console.WriteLine(mac);
            Console.WriteLine(ip);
            Console.WriteLine(ipsubnet);
            Console.WriteLine(ipgateway);
            Console.ReadKey();

        }

?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >