我正在尝试使用WMI和C#从远程计算机的事件查看器获取通知。我能够连接系统,也可以使用来获取事件日志ManagementObjectSearcher。但是,当我尝试使用ManagementEventWatcher.Start方法时,出现了一个异常:
访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))
我已经在WMI控制中root\cimv2赋予了权限,并且还赋予了DCOM Config中用户帐户的管理员权限。
我有普通的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
Run Code Online (Sandbox Code Playgroud)