Click here to Skip to main content
15,031,680 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to get all hostnames and ip addresses of computers connected to LAN using c# windows form application?
Posted
Comments
Henning Dieterichs 26-Oct-11 17:27pm
   
WTF? Look here: http://www.codeproject.com/Questions/274082/Find-host-name-and-ip-address-of-computers-connect

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

C#
DirectoryEntry netNode = new DirectoryEntry();
netNode.Path = "WinNT://Workgroup";
foreach (DirectoryEntry child in netNode.Children)
    {
    Console.WriteLine(child.Name);
    }
Where "Workgroup" is the name of your domain.
You will need to add a reference to System.DirectoiryServices, and the appropriate using as well.
   
C#
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 :
C#
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();
            }
        }
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900