Detailed description of winformc# get MAC address, IP address, subnet mask, default gateway code instance

Source: Internet
Author: User
The following small series for everyone to bring an article WinForm C # get the MAC address, IP address, subnet mask, the default gateway instance. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.


1. Add an assembly


2. Introduce namespaces

Using System.Management;

3. Methods

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];//默认网关
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.