马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现查看局域网电脑PC名和IP的几种方式

[复制链接]
查看7354 | 回复1 | 2012-12-1 14:02:56 | 显示全部楼层 |阅读模式
1.微软社区上介绍了使用Active Directory 来遍历局域网
利用DirectoryEntry组件来查看网络
网址:http://www.microsoft.com/china/communITy/program/originalarticles/techdoc/DirectoryEntry.mspx
  1. private void EnumComputers()
  2. {
  3.     using(DirectoryEntry root = new DirectoryEntry("WinNT:"))
  4.     {
  5.       foreach(DirectoryEntry domain in root.Children)
  6.       {
  7.         Console.WriteLine("Domain | WorkGroup: "+domain.Name);
  8.         foreach(DirectoryEntry computer in domain.Children)
  9.     {
  10.      Console.WriteLine("Computer: "+computer.Name);
  11.     }
  12.    }
  13. }
  14. }
复制代码
效果评价:速度慢,效率低,还有一个无效结果 Computer: Schema 使用的过程中注意虑掉。


2、利用Dns.GetHostByAddress和IPHostEntry遍历局域网
  1. private void EnumComputers()
  2. {
  3. for (int i = 1; i <= 255; i++)
  4. {
  5. string scanIP = "192.168.0." + i.ToString();
  6. IPAddress myScanIP = IPAddress.Parse(scanIP);
  7. IPHostEntry myScanHost = null;
  8. try
  9. {
  10.     myScanHost = Dns.GetHostByAddress(myScanIP);
  11. }
  12. catch
  13. {
  14.     continue;
  15. }
  16. if (myScanHost != null)
  17. {
  18.     Console.WriteLine(scanIP+"|"+myScanHost.HostName);
  19. }
  20. }
  21. }
复制代码
效果评价:效率低,速度慢,不是一般的慢。


3、使用System.Net.NetworkInformation.Ping来遍历局域网
  1. private void EnumComputers()
  2. {
  3. try
  4. {
  5.    for (int i = 1; i <= 255; i++)
  6.    {
  7.      Ping myPing;
  8.      myPing = new Ping();
  9.      myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
  10.      string pingIP = "192.168.0." + i.ToString();
  11.      myPing.SendAsync(pingIP, 1000, null);
  12.    }
  13. }
  14. catch
  15. {
  16. }
  17. }
  18. PRIVATE void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
  19. {
  20. if (e.Reply.Status == IPStatus.Success)
  21. {
  22.     Console.WriteLine(e.Reply.Address.ToString() + "|" + Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName);
  23. }
  24. }
复制代码
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
chao2332601 | 2013-6-15 23:55:30 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则