3

I have to find the machine names and the user names of those logged in to the machines.

  I have Updated My answer please see my answer !!!!

 NetworkBrowser nb = new NetworkBrowser();
 IPHostEntry ip = Dns.GetHostEntry(Dns.GetHostName());
 foreach (string pc in nb.getNetworkComputers())
 {
      cmbNetworkComputers.Items.Add(pc);
 }
 cmbNetworkComputers.SelectedIndex = 0;

The above code retrieves the machine names, how can I get the user names?

Aravind
  • 1,521
  • 2
  • 12
  • 23
  • possible duplicate of [How do I get the current username in .NET using C#?](http://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c) – CodeCaster Sep 24 '13 at 09:29
  • @CodeCaster That's not a duplicate of this question. The one you linked to is to retrieve the user name of the the account that is running the application. – user247702 Sep 24 '13 at 09:31
  • 1
    Then OP should elaborate on what exactly he wants and what has been tried. See for example [How to find username on remote computer?](http://superuser.com/questions/144685/how-to-find-username-on-remote-computer). – CodeCaster Sep 24 '13 at 09:33
  • @ Code Caster i dont need current machine name ... from network i have to find a machine and i need who is logged into that particular machine – Aravind Sep 24 '13 at 09:34
  • I need in C# ?? @ code caster – Aravind Sep 24 '13 at 09:35
  • Please explain what you mean with: Find Network Machine. – flayn Sep 24 '13 at 09:48
  • @ Florian I have a server and 22 machines connected to it . 22 machines are called network machines i want to find particular machine and who is logged in to that machine !!! – Aravind Sep 24 '13 at 10:14
  • Please see the answers i have put my answer but new problem came – Aravind Sep 26 '13 at 04:38

1 Answers1

1

See the answer at - How get list of local network computers? - for pointers on how to get a list of computers.

When you have the list you can use the WMI to query the computers for logged on users.

string comp = "Computer";
ConnectionOptions options = new ConnectionOptions();
//user (domain or local) with sufficient privileges to access the remote system through WMI
options.Username = "Usersname"; 
options.Password = "Password";
ManagementScope s = new ManagementScope(string.Format(@"\\{0}\root\cimv2", comp), options);

ManagementPath p = new ManagementPath(string.Format("Win32_ComputerSystem.Name='{0}'", comp));

using(ManagementObject cs = new ManagementObject (s, p, null ))
{
    cs.Get();
    Console.WriteLine(cs["UserName"]);
}
Community
  • 1
  • 1
Kami
  • 19,134
  • 4
  • 51
  • 63