2016-12-20 26 views
0

我可以列出以特定名称开头的网络中的所有计算机吗? 例如假设如下计算机共享网络 - (键盘,显示器,MONITOR1,monitor235,PC6,keyboard2,PC8,PC6,PC2)列出网络中以特定名称开头的所有计算机

我使用下面的代码列出的所有计算机的网络 -

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); 
      } 
     } 
    } 
} 

我可以列出所有以名称“PC”开头的PC吗? 即PC6,PC8,PC2

+0

您的实际问题是如何*匹配字符串*。谷歌搜索会给你很多结果('String.Contains','String.StartsWith','Regex.Match')。 – Groo

回答

1

为什么不使用Linq?

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

MSDN

相关问题