Home>Article>Backend Development> Detailed introduction to the code example of winformC# to obtain the Mac address, IP address, subnet mask, and default gateway (picture)

Detailed introduction to the code example of winformC# to obtain the Mac address, IP address, subnet mask, and default gateway (picture)

黄舟
黄舟 Original
2017-03-25 13:15:01 2428browse

The editor below will bring you an example of winformC# 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

##2. Introductionnamespace

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn