程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 獲得局域網中計算機的列表(包括計算機名,IP和MAC)的方法

獲得局域網中計算機的列表(包括計算機名,IP和MAC)的方法

編輯:.NET實例教程
有的時候需要根據MAC來限定登錄的計算機,為此查找了不少資料(有來自博客堂和CSDN),下面是獲得遠程計算機的Mac和局域網中計算機列表的方法。 需要引用的命名空間 using System; using System.Collections; using System.Diagnostics; using System.Management; using System.Net; using System.DirectoryServices; using System.Runtime.InteropServices; using System.Text.RegularExpressions; 獲得本機的MAC地址 public static string GetLocalMac() { string strMac = string.Empty; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach(ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) strMac += mo["MacAddress"].ToString() ; } return strMac.ToUpper(); } 獲得遠程計算機的MAC地址 方法一:使用API,利用ARP協議,只能獲得同網段計算機的MAC [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); [DllImport("Ws2_32.dll")] private static extern Int32 inet_addr(string ip); public static string GetRemoteMac(string clientIP) { string ip = clIEntIP; if ( ip == "127.0.0.1") ip = GetLocalIP()[0]; Int32 ldest=inet_addr(ip); Int64 macinfo=new Int64(); Int32 len=6; try { SendARP(ldest,0,ref macinfo,ref len); } catch { return ""; } string originalMACAddress = Convert.ToString(macinfo,16); if (originalMacAddress.Length<12) { originalMACAddress = originalMACAddress.PadLeft(12,''0''); } string macAddress; if(originalMACAddress!="0000" && originalMACAddress.Length==12) { string mac1,mac2,mac3,mac4,mac5,mac6; mac1=originalMACAddress.Substring(10,2); mac2=originalMACAddress.Substring(8,2); mac3=originalMACAddress.Substring(6,2); mac4=originalMACAddress.Substring(4,2); mac5=originalMACAddress.Substring(2,2); mac6=originalMACAddress.Substring(0,2); macAddress=mac1+"-"+mac2+"-"+mac3+"-"+mac4+"-"+mac5+"-"+mac6; } else { macAddress=""; } return macAddress.ToUpper(); } 方法二:使用windows的命令nbtstat public static string GetRemoteMacByNetBIOS(string clientIP) { string ip = clientIP; if ( ip == "127.0.0.1") ip = GetLocalIP()[0]; &n string dirResults=""; ProcessStartInfo psi = new ProcessStartInfo(); Process proc = new Process(); psi.FileName = "nbtstat.exe"; //psi.RedirectStandardInput = false; psi.RedirectStandardOutput = true;psi.RedirectStandardError=true; psi.Arguments = "-A " + ip; psi.UseShellExecute = false; proc = Process.Start(psi); dirResults = proc.StandardOutput.ReadToEnd(); string error = proc.StandardError.ReadToEnd(); proc.WaitForExit(); dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t",""); Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); Match mc=reg.Match(dirResults+"__MAC"); if(mc.Success) { return mc.Groups["key"].Value.ToUpper(); } else { return ""; } } 使用此方法需要足夠的操作系統的權限。在Web中,可以將ASP.net用戶加入管理員組。 對於上面兩個地方都用到的GetLocalIP是一個獲取本機IP的方法: public static string[] GetLocalIP() { string hostName = Dns.GetHostName(); IPHostEntry ipEntry=Dns.GetHostByName(hostName); IPAddress[] arr=ipEntry.AddressList; string[] result = new string[arr.Length]; for(int i=0;i { result[i] = arr[i].ToString(); } return result; } 獲得局域網內計算機的列表 方法一:使用逐個IP地址掃描的方式 利用多線程來對每個IP逐個掃描。 ComputerAddressInfo cai = new ComputerAddressInfo("192.168.1",42,53); Thread thScan = new Thread(new ThreadStart(cai.ScanComputers)); thScan.Start(); // public class ComputerAddressInfo { private int startIP = 0; private int endIP = 0; private string ipPrefix = ""; private ArrayList computerList = null; public ComputerAddressInfo(string ipPrefix,int startIP,int endIP) { this.startIP = startIP; this.endIP = endIP; this.ipPrefix = ipPrefix; computerList = new ArrayList(); } public void ScanComputers() { for(int i=startIP;i<=endIP;i++) { string scanIP = ipPrefix +"."+i.ToString(); IPAddress myScanIP = IPAddress.Parse(scanIP); IPHostEntry myScanHost = null; string[] arr = new string[2]; try { myScanHost = Dns.GetHostByAddress(myScanIP); } catch { continue; } if (myScanHost != null) { arr[0] = myScanHost.HostName; arr[1] = scanIP; computerList.Add(arr); } } } } 此方法速度比較慢。 方法二:使用Active Directory public static ArrayList GetComputerList() { ArrayList list = new ArrayList(); //or use "WinNT://your_domain_name" DirectoryEntry root = new DirectoryEntry("WinNT:"); DirectoryEntries domains = root.Children; domains.SchemaFilter.Add("domain"); foreach (DirectoryEntry domain in domains) { DirectoryEntries computers = domain.Children; computers.SchemaFilter.Add("computer"); foreach (DirectoryEntry computer in computers) { object[] arr = new string[3]; IPHostEntry iphe = null; try { iphe = Dns.GetHostByName(computer.Name); } catch { continue; arr[0] = domain.Name; arr[1] = computer.Name; if ( iphe != null && iphe.AddressList.Length >0 ) { for ( int i=0;i arr[2] += iphe.AddressList[i].ToString()+","; arr[2] = arr[2].ToString().Remove(arr[2].ToString().Length-1,1); } else arr[2] = ""; list.Add(arr); } } return list; &n } 此方法速度也比較慢。 後記 上面兩個獲得局域網內的計算機列表的方法都很費時,目前還沒有找到更好的辦法。 參考: http://blog.joycode.com/liuhuimiao/archive/2003/12/23/9754.ASPx
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved