获取“无效查询”ManagementException

时间:2015-04-01 06:41:32

标签: c# .net

我正在尝试获取域中机器的系统信息。 以下是我的代码。

ConnectionOptions opt = new ConnectionOptions();
opt.Authentication = System.Management.AuthenticationLevel.Packet;
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
opt.Username = strUserName;
opt.Password = strPassword;


ManagementPath p = new ManagementPath("\\\\" + strServerName + "\\root\\cimv2");

ManagementScope scope = new ManagementScope(p, opt);

ObjectQuery query = new ObjectQuery("SELECT Architecture FROM Win32_Processor");

ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection results = search.Get();
ManagementObjectCollection.ManagementObjectEnumerator e = results.GetEnumerator();
e.MoveNext();

ushort arch = (ushort)e.Current["Architecture"];

我在e.MoveNext()行上得到了无效查询的例外。这个代码几乎对所有机器运行都很好,但在某些机器上它给出了这样的例外。我不知道为什么会发生这种异常以及如何解决它?

0 个答案:

没有答案