在Windows窗体应用程序中查找使用c#连接到LAN的计算机的主机和ip地址 [英] Find host and ip address of computers connected to LAN using c# in Windows form applicatio

查看:72
本文介绍了在Windows窗体应用程序中查找使用c#连接到LAN的计算机的主机和ip地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#Windows窗体应用程序获取连接到LAN的计算机的所有主机名和IP地址?

how to get all hostnames and ip addresses of computers connected to LAN using c# windows form application?

推荐答案

我不能保证这将为您做所有的事情想要,但是:

I can''t guarantee this will do everything you want, but:

DirectoryEntry netNode = new DirectoryEntry();
netNode.Path = "WinNT://Workgroup";
foreach (DirectoryEntry child in netNode.Children)
    {
    Console.WriteLine(child.Name);
    }

工作组"是您的域的名称.
您将需要添加对System.DirectoiryServices的引用,以及相应的using.

Where "Workgroup" is the name of your domain.
You will need to add a reference to System.DirectoiryServices, and the appropriate using as well.


private StringCollection GetIps()
       {
           StringCollection Ips= new StringCollection();
           ProcessStartInfo calistir = new ProcessStartInfo("net", "view");
           calistir.CreateNoWindow = true;
           calistir.UseShellExecute = false;
           calistir.RedirectStandardOutput = true;
           Process calistirbasla = Process.Start(calistir);
           StreamReader oku = calistirbasla.StandardOutput;
           StringCollection isimler = MakineIsimleri(oku.ReadToEnd());
           foreach (string machine in isimler)
           {
               Ips.Add(IPAddresses(machine));

           }
           return Ips;
       }



您可以使用:
获取主机名



You can get hostnames with :

StringCollection isimler = MakineIsimleri(oku.ReadToEnd());


private StringCollection MakineIsimleri(string CalistirCiktisi)
        {
            string hepsi = CalistirCiktisi.Substring(CalistirCiktisi.IndexOf("\\"));
            StringCollection makineler = new StringCollection();
            while (hepsi.IndexOf("\\") != -1)
            {
                makineler.Add(hepsi.Substring(hepsi.IndexOf("\\"),
                    hepsi.IndexOf(" ", hepsi.IndexOf("\\")) - hepsi.IndexOf("\\")).Replace("\\", String.Empty));
                hepsi = hepsi.Substring(hepsi.IndexOf(" ", hepsi.IndexOf("\\") + 1));
            }
            return makineler;
        }



和:



And :

private  string IPAddresses(string server)
        {
            try
            {
                IPHostEntry heserver = Dns.Resolve(server);
                return heserver.AddressList[0].ToString();
            }
            catch(SocketException ex)
            {
                return MessageBox.Show(ex.Message+" Şu Serverda : "+server).ToString();
            }
        }


这篇关于在Windows窗体应用程序中查找使用c#连接到LAN的计算机的主机和ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆