我正在尝试使用 WMI 和 C# 从远程机器的事件查看器中获取通知。我能够连接系统并使用ManagementObjectSearcher
. 但是当我尝试使用ManagementEventWatcher.Start
方法时,我遇到了一个异常:
访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))
我已将 WMI 控制中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