扫描局域网IP地址和主机名的C#控制台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;

namespace CSHigher
{
    public class ShowIp
    {
        public static void Main()
        {
            EnumComputers();
            Console.ReadLine();
        }
        private static void EnumComputers()
        {
            try
            {
                for (int i = 1; i <= 255; i++)
                {
                    Ping myPing;
                    myPing = new Ping();
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

                    string pingIP = "192.168.163." + i.ToString();
                    myPing.SendAsync(pingIP, 1000, null);
                }
            }
            catch
            {
            }
        }

        private static void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            try
            {
                if (e.Reply.Status == IPStatus.Success)
                {
                    Console.WriteLine(e.Reply.Address.ToString() + " " + Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName);
                }
            }
            catch { }

        }
    }
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/58347.html

(0)
上一篇 2021年8月9日
下一篇 2021年8月9日

相关推荐

发表回复

登录后才能评论