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