I know how to get the process names that start on Windows, however, I am unable to get the process paths of the names...
Here's my attempt:
var mgmtWtch = new ManagementEventWatcher("Select * From Win32_ProcessStartTrace");
mgmtWtch.EventArrived += new EventArrivedEventHandler(mgmtWtch_EventArrived);
private void mgmtWtch_EventArrived(object sender, EventArrivedEventArgs e)
String ProcessName = e.NewEvent.Properties["ProcessName"].Value.ToString();
Process n = Process.GetProcessById(Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value));
String ProcessPath = n.MainModule.FileName;
MessageBox.Show(ProcessName + "\n" + ProcessPath);
When I start a process, it returns an exception on GetProcessById() that "No process with pid #### exists", even though it does. Maybe something to do with this being a 32 bit application and I'm running x64 processes? It did work with one x86 process. Other times it will throw an exception "Access Denied" on n.MainModule.FileName...
Hoping someone knows a better way to do this.