In C# how do I get the list of local computer names like what one gets viewing the Network in windows explorer

user334911 picture user334911 · Apr 7, 2011 · Viewed 12.7k times · Source

There are a lot of questions about getting the name and IP addresses of the local machine and several about getting IP addresses of other machines on the LAN (not all answered correctly). This is different.

In windows explorer if I select Network on the side bar I get a view of local machines on my LAN listed by machine name (in a windows workgroup, anyway). How do I get that same information programatically in C#?

Answer

Alex McBride picture Alex McBride · Apr 7, 2011

You can try using the System.DirectoryServices namespace.

var root = new DirectoryEntry("WinNT:");
foreach (var dom in root.Children) {
    foreach (var entry in dom.Children) {
        if (entry.Name != "Schema") {
            Console.WriteLine(entry.Name);
        }
    }
}