The editor below will bring you an example of winform C# obtaining the Mac address, IP address, subnet mask, and default gateway. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
1. Add an assembly
using System.Management;3. Method
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach (ManagementObject nic in nics) { if (Convert.ToBoolean(nic["ipEnabled"]) == true) { string mac = nic["MacAddress"].ToString();//Mac地址 string ip = (nic["IPAddress"] as String[])[0];//IP地址 string ipsubnet = (nic["IPSubnet"] as String[])[0];//子网掩码 string ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默认网关 } }
The above is the detailed content of Detailed introduction to the code example of winformC# to obtain the Mac address, IP address, subnet mask, and default gateway (picture). For more information, please follow other related articles on the PHP Chinese website!