Detect Exe start running and stop.

MiPakTeh 1,476 Reputation points
2021-07-13T03:23:04.633+00:00

Hi All,

I take somewhere this code to check if the program in my computer start running(exe.).When run this code error message;

System.Management.ManagementException
HResult=0x80131501
Message=Access denied
Source=System.Management
StackTrace:
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementEventWatcher.Start()
at _Exe_1.Form1..ctor() in C:\Users\family\source\repos_Exe_1_Exe_1\Form1.cs:line 24
at _Exe_1.Program.Main() in C:\Users\family\source\repos_Exe_1_Exe_1\Program.cs:line 19

This exception was originally thrown at this call stack:
[External Code]
_Exe_1.Form1.Form1() in Form1.cs
_Exe_1.Program.Main() in Program.cs

Here is a code;

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

namespace _Exe_1
{
    public partial class Form1 : Form
    {
        ManagementEventWatcher processStartEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace");
        ManagementEventWatcher processStopEvent = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace");
        public Form1()
        {
            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();

            Console.WriteLine("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();

            Console.WriteLine("Process stopped. Name: " + processName + " | ID: " + processID);
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-07-13T05:47:13.937+00:00

    This means that your project lacks permissions, please run your program with administrator permissions.

    When I did this, the error disappeared.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.