C # obtain local information and LAN information

Source: Internet
Author: User

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:
 




 

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.