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