我正在编写一个程序,该程序返回当前登录服务器的用户列表。
这是执行此操作的代码:
private List<string> GetUsers(string server)
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope moScope = new ManagementScope(@"\\" + server + @"\root\cimv2");
moScope.Connect();
ObjectQuery query = new ObjectQuery("select * from Win32_Process where name='explorer.exe'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(moScope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
ManagementOperationObserver mo = new ManagementOperationObserver();
mo.ObjectReady += new ObjectReadyEventHandler(mo_ObjectReady);
m.InvokeMethod(mo, "GetOwner", null);
}
}
private void mo_ObjectReady(object sender, ObjectReadyEventArgs e)
{
string user = e.NewObject.Properties["user"].Value.ToString();
//I would like to return the username back to the GetUsers function. How can I do this?
}
mo_ObjectReady 可以获取当前登录服务器的一个用户名。如何将值返回给 GetUsers 函数,以便我可以将它们完全放在一个列表中?
提前致谢。