Click here to Skip to main content
15,890,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have some code on a small project where I want to find all the machines on my network. I need to ultimately get the hostnames and currently, I use Directory Services and then drill down to the child entries to find the information I need. This has worked great for about a year on multiple machines on different networks.

I have started to find though that more and more machines are not finding anything even though explorer will see them fine. I even think machines which previously found things now don't so I'm wondering if this is a windows update which has left me in trouble.

My question therefore is has anyone else experienced this and if so is there a resolution? Is there something better to use than Directory Services (i did use to find it a little slow anyway)

private void GetMachines()//(object sender, EventArgs e)
       {

           string MachineDetails;
           int MachineCheckBox = 0;
           string MyMachineName = Environment.MachineName;

           if (System.Net.Dns.GetHostName().Length < 16)
           {
               System.Net.Dns.GetHostName().ToString();
           }
           else
           {
               System.Net.Dns.GetHostName().Substring(0, 15).ToUpper();
           }

           DirectoryEntry root = new DirectoryEntry("WinNT:");
           foreach (DirectoryEntry computers in root.Children)
           {
               foreach (DirectoryEntry computer in computers.Children)
               {
                   string ComputerName = computer.Name;
                   if ((computer.Name != "Schema") && (ComputerName != MyMachineName))// "Composer_A"))
                   {

                       MachineCheckBox = MachineCheckBox + 1;

                       CheckBox box;

                       MachineDetails = computer.Name;// +"\r\n";
                       box = new CheckBox();
                       IPAddress[] ipaddress = Dns.GetHostAddresses(ComputerName);
                       foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
                       {
                           box.Tag = ip4;
                       }
                       System.Net.Dns.GetHostAddresses(ComputerName);


                       box.Text = MachineDetails;
                       box.Checked = true;
                       box.AutoSize = true;
                       box.Location = new Point(10, MachineCheckBox * CheckBoxSpacing); //vertical
                       //box.Location = new Point(i * 50, 10); //horizontal
                       //this.Controls.Add(box);
                       grpMachines.Controls.Add(box);

                   }
               }
               int groupwidth = grpRemovableDrives.Size.Width;
               int grpMachinesHeight = grp1stControl + (MachineCheckBox * CheckBoxSpacing);
               if (grpMachinesHeight < 36)
               {
                   grpMachinesHeight = 36;
               }
               grpMachines.Size = new Size(groupwidth, grpMachinesHeight);
               this.Size = new Size(this.Size.Width, (grpMachines.Location.Y + grpMachines.Size.Height) + 50);

           }
       }


What I have tried:

Stepping through there are no errors just the count = 0 under the child. on my test system windows explorer shows 3 machines all with working shares (if shares makes a difference)
Posted
Updated 22-Aug-19 10:55am

1 solution

I have this: Retrieving IP and MAC addresses for a LAN[^] and once you have the IP address, you can use Dns.GetHostEntry Method (System.Net) | Microsoft Docs[^] to retrieve the IpHostEntry for the system, which includes the HostName.
 
Share this answer
 
Comments
Member 11644953 22-Aug-19 16:59pm    
Hi
Sorry - I haven't ignored your answer but I'm struggling to shoehorn it into my code. Think I must be misunderstanding something but will keep plugging.

Thank you for the suggestion

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