2016-12-20 27 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

相關問題