The Management class is required for obtaining local information and LAN information. This class has powerful functions and can implement many things, below I will list some examples of local and local area network information.
Add a System. Management reference to the project, and enter using System. Management in the code.
Let's take a look at how to obtain the local information:
Private static string ipAddress = "";
Static void Main (string [] args)
{
String macAddress = "";
ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection mac = mc. GetInstances ();
Foreach (ManagementObject mo in mac)
{
If (bool) mo ["IPEnabled"] = true)
{
MacAddress + = mo ["MacAddress"]. ToString ();
}
}
MacAddress = macAddress. Replace (:,-);
IPAddress [] ipAddressList = Dns. GetHostByName (Dns. GetHostName (). AddressList;
For (int I = 0; I <ipAddressList. Length; I ++)
{
IpAddress + = ipAddressList [I]. ToString ();
}
Console. WriteLine (macAddress); // MacAddress
Console. WriteLine (ipAddress); // IPAddress
Console. WriteLine (Dns. GetHostName (); // HostName
Console. WriteLine (Environment. UserName); // UserName
Console. WriteLine (Environment. UserDomainName); // DomainName
Console. ReadLine ();
}
The code above will output the following information:
00-1E-C9-29-59-D7
192.168.1.1
NON-Alex
Alex
CompanyLan
The following is an IP address used to determine the hosts in the local area network: