21

I am trying to get a list of local network computers. I tried to use NetServerEnum and WNetOpenEnum API, but both API return error code 6118 (ERROR_NO_BROWSER_SERVERS_FOUND). Active Directory in the local network is not used.

Most odd Windows Explorer shows all local computers without any problems.

Are there other ways to get a list of computers in the LAN?

KindDragon
  • 6,558
  • 4
  • 47
  • 75
  • This is the same question basically: [http://stackoverflow.com/questions/105676/get-a-list-of-all-computers-on-a-network-w-o-dns](http://stackoverflow.com/questions/105676/get-a-list-of-all-computers-on-a-network-w-o-dns) – Avitus Apr 01 '10 at 01:08
  • 5
    No. Nmap is not suitable for me. Parse output from other program is not very good – KindDragon Apr 01 '10 at 11:39

6 Answers6

16

You will need to use the System.DirectoryServices namespace and try the following:

DirectoryEntry root = new DirectoryEntry("WinNT:");

foreach (DirectoryEntry computers in root.Children)
{
    foreach (DirectoryEntry computer in computers.Children)
    {
        if (computer.Name != "Schema")
        {
             textBox1.Text += computer.Name + "\r\n";
        }
    }
}

It worked for me.

RoastBeast
  • 1,059
  • 2
  • 22
  • 38
Cynfeal
  • 259
  • 3
  • 6
12

I found solution using interface IShellItem with CSIDL_NETWORK. I get all network PC.

C++: use method IShellFolder::EnumObjects

C#: you can use Gong Solutions Shell Library

using System.Collections;
using System.Collections.Generic;
using GongSolutions.Shell;
using GongSolutions.Shell.Interop;

    public sealed class ShellNetworkComputers : IEnumerable<string>
    {
        public IEnumerator<string> GetEnumerator()
        {
            ShellItem folder = new ShellItem((Environment.SpecialFolder)CSIDL.NETWORK);
            IEnumerator<ShellItem> e = folder.GetEnumerator(SHCONTF.FOLDERS);

            while (e.MoveNext())
            {
                Debug.Print(e.Current.ParsingName);
                yield return e.Current.ParsingName;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
KindDragon
  • 6,558
  • 4
  • 47
  • 75
8

I made a function out of it. The SchemaClassName has to be Computer

    public List<string> NetworkComputers()
    {
        return (
        from Computers 
        in (new DirectoryEntry("WinNT:")).Children
        from Computer 
        in Computers.Children
        where Computer.SchemaClassName == "Computer"
        orderby Computer.Name
        select Computer.Name).ToList;
    }
toddmo
  • 20,682
  • 14
  • 97
  • 107
  • This one works. Essentially its the line `where Computer.SchemaClassName == "Computer"` which does Filter out every group and user in the network, which still will be listed in @Cynfeal 's anwer, until it's beeing changed. – LuckyLikey Jun 24 '15 at 10:04
  • I had to add `.Cast()` to the `Children` collection to make it enumerable. – Olivier Jacot-Descombes Dec 19 '22 at 13:23
3

Here a property that uses a LINQ query

private List<string> NetworkHosts
    {
        get
        {
            var result = new List<string>();

            var root = new DirectoryEntry("WinNT:");
            foreach (DirectoryEntry computers in root.Children)
            {
                result.AddRange(from DirectoryEntry computer in computers.Children where computer.Name != "Schema" select computer.Name);
            }
            return result;
        }
    }
Jonas_Hess
  • 1,874
  • 1
  • 22
  • 32
2

A minor extension to toddmo's answer, if you don't really like LINQ query style syntax and want to also include workgroups as an option:

public IEnumerable<string> VisibleComputers(bool workgroupOnly = false) {
        Func<string, IEnumerable<DirectoryEntry>> immediateChildren = key => new DirectoryEntry("WinNT:" + key)
                .Children
                .Cast<DirectoryEntry>();
        Func<IEnumerable<DirectoryEntry>, IEnumerable<string>> qualifyAndSelect = entries => entries.Where(c => c.SchemaClassName == "Computer")
                .Select(c => c.Name);
        return (
            !workgroupOnly ?
                qualifyAndSelect(immediateChildren(String.Empty)
                    .SelectMany(d => d.Children.Cast<DirectoryEntry>())) 
                :
                qualifyAndSelect(immediateChildren("//WORKGROUP"))
        ).ToArray();
    }
Aldous Zodiac
  • 601
  • 3
  • 5
1

Solution with LINQ lambda syntax and unmanaged resource resolver

 public List<String> GetNetworkHostNames()
 {
        using (var directoryEntry = new DirectoryEntry("WinNT:"))
        {
            return directoryEntry
                     .Children
                     .Cast<DirectoryEntry>()
                     .SelectMany(x=>x.Children.Cast<DirectoryEntry>())
                     .Where(c => c.SchemaClassName == "Computer")
                     .Select(c => c.Name)
                     .ToList();
        }
 }
WhoKnows
  • 340
  • 1
  • 12