Search code examples
c#.netnetwork-programmingdevice-discovery

c# network discovery


I'm developing a C# application and I need to discover computers and printers in the LAN. How can I do this from .NET? Is there any class in .NET framework I can use. Thanks for answers.


Solution

  • You can use DirectoryServices to get computers.

        System.DirectoryServices.DirectoryEntry winNtDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
        List<String> computerNames = (from DirectoryEntry availDomains in winNtDirectoryEntries.Children 
                                      from DirectoryEntry pcNameEntry in availDomains.Children 
                                      where pcNameEntry.SchemaClassName.ToLower().Contains("computer") 
                                      select pcNameEntry.Name).ToList();