c#扫描网段内计算机

2012.02.17 2 Comments 围观人数:305人
  1. //获取本地机器名    
  2. string _myHostName = Dns.GetHostName();   
  3. //获取本机IP    
  4. string _myHostIP = Dns.GetHostEntry(_myHostName).AddressList[0].ToString();   
  5. //截取IP网段   
  6. string ipDuan = _myHostIP.Remove(_myHostIP.LastIndexOf('.'));   
  7. //枚举网段计算机   
  8. for (int i = 1; i <= 255; i++)   
  9. {   
  10.     Ping myPing = new Ping();   
  11.     myPing.PingCompleted +=   
  12. pletedEventHandler(_myPing_PingCompleted);   
  13.     string pingIP = ipDuan + "." + i.ToString();   
  14.     myPing.SendAsync(pingIP, 1000, null);   
  15. }  
  1. void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)   
  2.         {   
  3.             if (e.Reply.Status == IPStatus.Success)   
  4.             {   
  5.                 listBox1.Items.Add(e.Reply.Address.ToString());   
  6.             }   
  7.         }  
2 Responses
Comment (2)
Trackback (0)
  • #1
    losable :

    多网卡呢?如果路由器忽略PING呢?嘿嘿……

    2012.02.17 18:14 Reply
  • 还没有Trackback
Leave a Reply
icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif