1

Can I list all the computers which is in network which starts with particular name? e.g. suppose below given computers are shared in network- (keyboard, monitor, monitor1, monitor235, PC6, keyboard2, PC8, PC6, PC2)

I am using below code to list all the computers in network-

List<string> list = new List<string>();
using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
{
    foreach (DirectoryEntry computers in root.Children)
    {
        foreach (DirectoryEntry computer in computers.Children)
        {
            if ((computer.Name != "Schema"))
            {
                list.Add(computer.Name);
            }
        }
    }
}

can I list all the PC which starts with name "PC"? i.e PC6, PC8, PC2

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Your actual problem is how to *match strings*. Googling for that would give you numerous results (`String.Contains`, `String.StartsWith`, `Regex.Match`). – vgru Dec 20 '16 at 17:20

1 Answers1

1

Why not use Linq?

root.Children
    .SelectMany(x => x.Children)
    .Where(x => x.Name.StartsWith("PC"))
    .Select(x => x.Name);

See MSDN

haim770
  • 48,394
  • 7
  • 105
  • 133