Question: this is a c# program for some reason this is supposed to display processes on a richtextbox on a form but every time i run

this is a c# program for some reason this is supposed to display processes on a richtextbox on a form but every time i run the program it gets access denied below is the code

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

namespace WindowsFormsApp5 { public partial class Form1 : Form { struct APPINFO { // Unique identifier public int processID;

public string processName; public string processPath; public DateTime startTime; }

private readonly List currentlyRunningProcesses = new List(); public Form1() { InitializeComponent(); try { var AppStartQuery = new WqlEventQuery("Win32_ProcessStartTrace");

using (var Startwatcher = new ManagementEventWatcher(AppStartQuery)) { Startwatcher.EventArrived += new EventArrivedEventHandler(StartedProcess); Startwatcher.Start(); }

/* var AppStopQuery = new WqlEventQuery("Win32_ProcessStopTrace");

using (var Stopwatcher = new ManagementEventWatcher(AppStopQuery)) { Stopwatcher.EventArrived += new EventArrivedEventHandler(StoppedProcess); Stopwatcher.Start(); }*/

} catch(Exception ex) { MessageBox.Show(ex.Message); } }

private void StartedProcess(object sender, EventArrivedEventArgs e) { try { var processStart = Process.GetProcessById(Convert.ToInt32(e.NewEvent.Properties["processID"].Value.ToString())); var appInfo = new APPINFO { processID = Process.GetProcessById(processStart.Id).Id, processName = Process.GetProcessById(processStart.Id).ProcessName, processPath = Process.GetProcessById(processStart.Id).MainModule.FileName, startTime = Process.GetProcessById(processStart.Id).StartTime }; RunningProcesses(); } catch (ArgumentException) { } catch (Win32Exception) { } }

private void RunningProcesses() { if (InvokeRequired) { BeginInvoke(new Action(() => { localProcessesRichTextBox.Clear(); foreach (APPINFO runningProcess in currentlyRunningProcesses) { if (runningProcess.processPath != "Web Process") { localProcessesRichTextBox.AppendText(runningProcess.processName + " "); } } })); } }

private void Form1_Load(object sender, EventArgs e) {

} } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!