Get LAN computer name and IP and MAC address

Source: Internet
Author: User

 voidGetAll () {IDictionary<string,string> hostList =Newdictionary<string,string>(); DirectoryEntry Maingroup=NewDirectoryEntry ("WinNT:"); foreach(DirectoryEntry DomaininchMaingroup.children) {foreach(DirectoryEntry pcinchdomain. Children) {Try{system.net.ipaddress[] remotehost=dns.gethostaddresses (PC.                        Name); Hostlist.add (PC. Name, remotehost[0].                    ToString ()); }                    Catch { }                }            }            using(StreamWriter SW =NewStreamWriter ("C:\\pp.txt",true))            {                foreach(keyvaluepair<string,string> ChildinchhostList) {SW. WriteLine (Environment.NewLine+"Name:"+ Child. Key +"IP:"+Child .                Value); } SW.                Flush (); Sw.            Close (); }        }
View Code

The above to save the LAN computer and IP to the C drive

Then get the MAC address based on the IP address

  Public stringGetmac (stringIP) {            stringDirresults =""; ProcessStartInfo PSI=NewProcessStartInfo (); Process proc=NewProcess (); Psi. FileName="nbtstat"; Psi. Redirectstandardinput=false; Psi. Redirectstandardoutput=true; Psi. Arguments="-A"+IP; Psi. UseShellExecute=false; Proc=Process.Start (PSI); Dirresults=Proc.            Standardoutput.readtoend (); Proc.            WaitForExit (); Dirresults= Dirresults.replace ("\ r",""). Replace ("\ n",""). Replace ("\ t",""); Regex Reg=NewRegex ("mac[]{0,}address[]{0,}=[]{0,} (?<key> (.) *?)) __mac", Regexoptions.ignorecase |regexoptions.compiled); Match MC= Reg. Match (Dirresults +"__mac"); if(MC. Success) {returnMc. groups["Key"]. Value; }            Else{reg=NewRegex ("Host not found", Regexoptions.ignorecase |regexoptions.compiled); MC=Reg.                Match (Dirresults); if(MC. Success) {return "Host not found!"; }                Else                { return ""; } }        }
View Code

Need a little change to use

Get LAN computer name and IP and MAC address

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.