2013-09-24 181 views
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; 

上面的代碼檢索機器名稱,我如何獲取用戶名?

+0

可能重複的[我如何使用C#在.NET中獲取當前用戶名?](http://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username -in-net-using-c) – CodeCaster

+0

@CodeCaster這不是這個問題的重複。您鏈接的是檢索運行應用程序的帳戶的用戶名。 – Stijn

+1

然後OP應該詳細說明他到底想要什麼以及嘗試過什麼。請參閱例如[如何在遠程計算機上查找用戶名?](http://superuser.com/questions/144685/how-to-find-username-on-remote-computer)。 – CodeCaster

回答

1

請參閱答案 - How get list of local network computers? - 有關如何獲取計算機列表的指示信息。

當您擁有該列表時,您可以使用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"]); 
}