2011-10-19 16 views
3

如何查詢網絡中的所有PC以查看誰實際登錄。我需要獲取「IPAddress + LogedUserName」的列表。c#檢查網絡上登錄的用戶

我正在電腦上可用的局域網通過這樣的名單:

using (DirectoryEntry root = new DirectoryEntry("WinNT:")) 
     { 
      foreach (DirectoryEntry computers in root.Children) 
      { 
       foreach (DirectoryEntry computer in computers.Children) 
       { 
        if ((computer.Name != "Schema")) 
        { 
         textBox1.Text += computer.Name + "\r\n"; 
        } 
       } 
      } 
     } 

但我想也有每個可用計算機上登錄的用戶名。

+0

您是否是域名?你需要提供更多的細節,因爲這個問題很可能會被關閉。 –

+0

編輯了這個問題,好嗎?我想檢查IP地址範圍到電腦的可用性,得到它的名字,並獲得當前loged用戶名.. – DefinitionHigh

+0

好多了,我投票重新打開。 –

回答

1

現在您的問題已重新打開,這裏是使用cassia的更完整解釋。這可能會或可能不會在沒有啓用RemoteDesktop的桌面上運行。這段代碼完全沒有經過測試,它可能需要一些修補程序才能100%工作,但它會讓你走上正軌。

using (DirectoryEntry root = new DirectoryEntry("WinNT:")) 
{ 
    ITerminalServicesManager tsm = new Cassia.TerminalServicesManager(); 

    foreach (DirectoryEntry computers in root.Children) 
    foreach (DirectoryEntry computer in computers.Children) 
    { 
     if ((computer.Name != "Schema")) 
     { 
      string linqCapture = computer.Name; //<-- This may not be necessary, 
               //but I have always have had bad 
               //experiences with LINQ and foreach 
               //loops not capturing the current 
               //value of the variable correctly. 

      //remove the last Where clause if you want all users connected 
      //to the computer, not just the one where it is the console session. 
      foreach(var session in tsm.GetSessions(linqCapture) 
             .Where(s => s.ConnectionState == ConnectionState.Active) 
             .Where(s => s.ClientName == linqCapture)) 
      { 
       string LoggedInUser = session.UserName; 
       System.Net.IPAddress LoggedInIp = session.ClientIPAddress; 
       //Do with data what ever you want to; 
      } 
     } 
    } 
} 
+0

嗨斯科特張伯倫,我有一個相同的問題,我認爲你的答案是我的解決方案,所以我試過你的代碼,但它不會返回任何東西!你有沒有什麼建議?該代碼應該在服務器上運行? –

+0

我會打開一個新問題,準確顯示您所做的以及您在此過程中獲得的任何錯誤消息。一定要包括什麼版本的.NET和你正在運行你的程序的操作系統。 –

+0

我曾問過我的問題已在http://stackoverflow.com/questions/14302861/getting-a-list-of-every-active-computers-on-the-network但沒有人回答。請幫助我... –