Reputation: 1549
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?
Upvotes: 3
Views: 3851
Reputation: 19447
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"]);
}
Upvotes: 1