2016-12-20 4 views
0

特定の名前で始まるネットワークにあるすべてのコンピュータをリストできますか?例: 特定のコンピュータがネットワーク - (キーボード、モニター、モニタ1、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

関連する問題