hello to all

In my code I have to check currently running processes from task manager and perform some actions on them but the problem I am facing in doing so is that I am not able to get a processes location from where it is running (i.e. path of exe file). Can any cody help me in this regard.


7 Years
Discussion Span
Last Post by abelLazm

So assuming you have an array of all processes, in your foreach

foreach(Process p in processes)
   string path = p.MainModule.FileName;

i have tried this code but it is not working for me. Following exception encounters

System.ComponentModel.Win32Exception was unhandled
  Message=Access is denied

Details of encountered Exception

System.ComponentModel.Win32Exception was unhandled
  Message=Access is denied
       at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
       at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
       at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
       at System.Diagnostics.Process.get_MainModule()
       at CheckRunningProcesss.Form1.IsProcessOpen(String name) in D:\Projects\CheckRunningProcesss\CheckRunningProcesss\Form1.cs:line 35
       at CheckRunningProcesss.Form1.button1_Click(Object sender, EventArgs e) in D:\Projects\CheckRunningProcesss\CheckRunningProcesss\Form1.cs:line 62
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at CheckRunningProcesss.Program.Main() in D:\Projects\CheckRunningProcesss\CheckRunningProcesss\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

i am checking paths of all of the processes running right now. but my aim is to check open websites, documents (.doc, .ppt, .pdf, .xls etc), images and exe files started by user


Thank you Momerath :)... your post made me check my code and it is solved now. the exception was because i was checking paths of process running from server of my office on which of course i don't have permission to check path :p any way thanx

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.