C # get local IP address and MAC address

Source: Internet
Author: User
Tags aliases get ip
C # get local IP Address and MAC Address   1, obtain the LAN IP address through the host name;             try              {                 iphostentry iphostentry = Dns.gethostentry (Txthost.text);//pass Computer name                  if (ipHostEntry.Aliases.Length > 0)                 {                     foreach (string Alias in iphostentry.aliases)                          txtip.text = alias;                }                 //Get IP address                  foreach (IPAddress addr in iphostentry.addresslist)                      Txtipaddress.text = addr. ToString ();//Get IP address             }              Catch              {                MessageBox.Show ("Wrong host name.") ");            } 2, get host name via LAN IP address; try              {                iphostentry iphostentry = dns.gethostbyaddress (Txtip.text); ;//Pass IP address                txthostname.text = IpHostEntry.HostName.ToString ()//Get host name            }             Catch              {                MessageBox.Show ("Wrong IP Address"). ");            } 3, get MAC address public static string Readmac via LAN IP address (string IP)//IP address, you can return the MAC address         {             string mac = "";             System.Diagnostics.Process p = new System.Diagnostics.Process ();             p.startinfo.filename = "nbtstat";             p.startinfo.arguments = "-a" + IP;             P.startinfo.useshellexecute = false;             P.startinfo.createnowindow = true;             P.startinfo.redirectstandardoutput = true;                           P.start ();             string output = P.standardoutput.readtoend ();             int len = output. IndexOf ("MAC address =");             if (len>0)              {             & NBsp;  mac = output. Substring (len + 14, 17);            }                          p.WaitForExit ( );                          return mac;        }  

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.