我试图从使用WMI和C#的远程计算机的事件查看器中获取通知。我可以通过使用ManagementObjectSearcher
连接系统并获取事件日志。但是当我尝试使用ManagementEventWatcher.Start
方法时,我收到一个异常:ManagementEventWatcher(WMI)异常通知远程计算机的事件
访问被拒绝。 (异常来自HRESULT:0X80070005 (E_ACCESSDENIED))
我已经给在WMI控制的permisions到root\cimv2
,并且还给出了管理权限,以在DCOM配置用户的帐户。
我有正常的Windows应用程序,因此我没有使用ASP.net(ASPNET用户)在我的情况。
我的代码是:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = @"Domain\UName";//txtUserName.Text;
connectionOptions.Password = "pass";//txtPassword.Text;
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope managementScope = new ManagementScope(@"\\server\root\cimv2",connectionOptions);
managementScope.Options.EnablePrivileges = true;
managementScope.Connect(); // this line is executing fine.
eventWatcher = new ManagementEventWatcher(managementScope, new EventQuery("Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Application'"));
eventWatcher.EventArrived += new EventArrivedEventHandler(Arrived);
eventWatcher.Scope.Options.EnablePrivileges = true;
eventWatcher.Start(); // Error occurs here
这显然应该是被接受的答案。我花了很多时间来解决这个错误,异步版本根本无法使用身份验证。谢谢你的回答。 – 2011-05-23 13:33:56
@Pieter - 这是可能的,看到我的答案。尽管如此,微软仍然推荐使用半同步方法。 – 2012-02-15 22:55:42