Search code examples
c#.netsystem.managementmanagementeventwatcher

ManagementEventWatcher.Start() Access Denied


I'm trying to create an application in C#.Net, and I need it to scan the processes that the user start and stop, but I'm getting an "Access Denied" on the .Start()

Here is what I've got so far

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);
        }
    }
}

I've searched online hours, but couldn't find anything. People that had this problem, had it remotelly, and not locally. I am using Windows Pro 8.1 + Microsoft Visual Studio Ultimate 2013, just in case that versions of VS or OS may be important in this case.

The problem triggers on processStartEvent.Start() saying "Access denied". I also tried to switch from .Start() to .WaitForNextEvent() with the same result

Anything that I'm missing here?


Solution

  • ClickOnce was activated. Disabling it solved my problem. Thanks tho.