Home > Backend Development > C#.Net Tutorial > 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)

黄舟
Release: 2017-03-25 13:15:01
Original
2651 people have browsed it

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

##2. Introduction namespace

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];//默认网关
}
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template