ManagementEventWatcher.Start()访问被拒绝

时间:2015-06-30 00:45:47

标签: c# .net system.management managementeventwatcher

我试图在C#.Net中创建一个应用程序,我需要它来扫描用户启动和停止的进程,但我得到了一个"访问被拒绝"在.Start()

这是我到目前为止所做的事情

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using MySql.Data.MySqlClient;
using System.Management;
using Security;
using MySQLConnectivity;

namespace MyApp
{
    public partial class Login : Form
    {
        MySQLClass sql = new MySQLClass();
        ManagementEventWatcher processStartEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace");
        ManagementEventWatcher processStopEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace");

        public Login()
        {
            InitializeComponent();
            processStartEvent.EventArrived += new EventArrivedEventHandler(processStartEvent_EventArrived);
            processStartEvent.Start();
            processStopEvent.EventArrived += new EventArrivedEventHandler(processStopEvent_EventArrived);
            processStopEvent.Start();
        }

        void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e)
        {
            string processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
            string processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value).ToString();

            MessageBox.Show("Process started. Name: " + processName + " | ID: " + processID);
        }

        void processStopEvent_EventArrived(object sender, EventArrivedEventArgs e)
        {
            string processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
            string processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value).ToString();

            MessageBox.Show("Process stopped. Name: " + processName + " | ID: " + processID);
        }
    }
}

我已经在线搜索了一些小时,但却找不到任何东西。有这个问题的人,有远程,而不是本地。我正在使用Windows Pro 8.1 + Microsoft Visual Studio Ultimate 2013,以防VS或OS的版本在这种情况下很重要。

问题触发processStartEvent.Start()说"访问被拒绝"。 我还尝试使用相同的结果从.Start()切换到.WaitForNextEvent()

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

ClickOnce已激活。禁用它解决了我的问题。谢谢你。