我有一个.NET应用程序,它在所有域计算机上运行WMI查询,以便找到登录用户;它ping每台计算机以查找它是否在线,然后运行实际查询。
代码段:
try
{
string loggedonuser = null;
string computername = "ComputerToQuery";
ConnectionOptions co = new ConnectionOptions();
co.Username = "DOMAIN\MyUser";
co.Password = "MyPassword";
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.Default;
ManagementPath mp = new ManagementPath(@"\\" + computername + @"\root\cimv2");
ManagementScope ms = new ManagementScope(mp,co);
ms.Connect();
ObjectQuery oq = new ObjectQuery("SELECT username FROM Win32_ComputerSystem");
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms,oq);
foreach(ManagementObject mo in mos.Get())
loggedonuser = (String) mo["username"];
}
catch(Exception e)
{
// Handle WMI exception
}
问题:有时WMI查询会无限期地挂起。
如何设置超时?