3

我必须找到机器名称和登录机器的用户名。

  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;

上面的代码检索机器名,如何获取用户名?

4

1 回答 1

1

请参阅 -如何获取本地网络计算机列表?- 有关如何获取计算机列表的指示。

当您拥有该列表时,您可以使用 WMI 查询计算机的登录用户。

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"]);
}
于 2013-09-24T10:00:19.123 回答