question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked MiPakTeh-6272 commented

Detect Exe start running and stop.

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)
         {
    
         }
     }
 }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered MiPakTeh-6272 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you TimonYang.

It true disappear when run in Adminstrator.Have other code can be run without Adminstartor .??

0 Votes 0 ·