I have been working on this for the last week and I’ve tried a ton of different approaches and haven’t found anything that works consistently.
Essentially, I am trying to build my own FolderDialog and I have local drives working perfectly, but I can’t get network drives / shares to work…
I figured I would break this down into smaller parts to try to build out the feature, but even there I’m stuck.
So I’m trying to build a code snippet that will return a list of the Computer Names, IP Addresses, and UNC Paths of other computers on the network.
Nothing I’ve tried has worked and I’ve tried many different approaches (I’ll detail some of them below).
I understand that the approach for retrieving network computers on a domain is going to be different from a non-domain ad-hoc network, so ideally I would like something that works for both in some fashion…
But has anyone found an approach for getting a list of computers on the network that works and is efficient? Here are some of the approaches I’ve tried. None of them worked.
I really appreciate the help!
1)
DataTable resultDT = new DataTable();
resultDT.Columns.Add("ComputerName", typeof(string));
resultDT.Columns.Add("ComputerUNCPath", typeof(string));
resultDT.Columns.Add("IPAddress", typeof(string));
string WG = ComputerWorkgroupNameString(); //This does work.
try
{
// Create a directory entry for the WinNT provider with the workgroup name
DirectoryEntry workgroup = new DirectoryEntry("WinNT://" + WG);
foreach (DirectoryEntry child in workgroup.Children)
{
// Filter out non-computer entries (e.g., schemas)
if (child.SchemaClassName == "Computer")
{
// Retrieve IP address (if available)
var ipAddress = child.Properties["IpAddress"].Value;
resultDT.Rows.Add(child.Name, "", ipAddress);
}
}
}
catch (Exception ex)
{
}
try
{
Domain domain = Domain.GetCurrentDomain();
DomainControllerCollection domainControllers = domain.DomainControllers;
foreach (DomainController dc in domainControllers)
{
resultDT.Rows.Add(dc.Name, "", dc.IPAddress);
}
}
catch (ActiveDirectoryOperationException ex)
{
}
// Get all network interfaces
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in networkInterfaces)
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
resultDT.Rows.Add(Environment.MachineName, "", nic.GetIPProperties().UnicastAddresses[0].Address);
}
}
// Create a directory entry for the WinNT provider
DirectoryEntry root = new DirectoryEntry(“WinNT:”);
// Iterate through the children (computers) in the root
foreach (DirectoryEntry computers in root.Children)
{
foreach (DirectoryEntry computer in computers.Children)
{
// Filter out non-computer entries (e.g., schemas)
if (computer.SchemaClassName == "Computer")
{
//Console.WriteLine(computer.Name);
resultDT.Rows.Add(computer.Name, "");
}
}
}