2012-04-19 79 views
3

我想知道是否有一種方法可以獲取使用C#在我的網絡位置顯示的所有計算機名稱。從我的網絡位置獲取計算機名稱

+1

Duplicate question:http://stackoverflow.com/questions/5581140/in-c-sharp-how-do-i-get-the-list-of-local-computer-names-like-what-one- gets-view – 2012-04-19 20:34:35

+0

甚至是那個顯然是從http://stackoverflow.com/questions/2557551/how-get-list-of-local-network-computers/2562302#2562302被騙的人。 – 2012-04-19 20:35:55

回答

3

您將需要使用NetServerEnum()API。我不相信這是在基本.NET庫這個託管的包裝,但我可以用快速谷歌搜索發現這一點:http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using

:我沒有測試或徹底審查CodeProject上的代碼,但如果有任何問題,它應該足以作爲你所需要的起點。

編輯:除非確信域環境,否則請勿使用DirectoryServices。 System.DirectoryServices類是一個ADSI包裝器,無需Active Directory即可進行查詢。 NetServerEnum()適用於工作組和域,但不保證最可靠的數據(並非所有機器都可能顯示)。它依賴於計算機瀏覽器服務。

最好的解決方案很可能是一個包裝兩種可能性和合並結果的類:/

+0

這可能是一個非常愚蠢的問題,但是,僅僅執行'net view> computerListing.txt'使用StreamReader讀取它並完成它會不會更容易? – 2014-01-31 09:06:19

+0

非常感謝!你的解釋真的幫助我理解! – 2018-01-01 10:29:12

3

這可行,但需要一段時間。 :/

public List<String> ListNetworkComputers() 
{ 
    List<String> _ComputerNames = new List<String>(); 
    String _ComputerSchema = "Computer"; 
    System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:"); 
    foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children) { 
     foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children) { 
      if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower())) { 
      _ComputerNames.Add(_PCNameEntry.Name); 
      } 
     } 
    } 
    return _ComputerNames; 
} 
2

取決於用戶的權限,應用程序可能會或可能不會得到這些信息。

嘗試使用ActiveDirectory。這應該會爲您提供關於本地網絡的準確信息。

使用System.DirectoryServices。