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.

Thanx

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
  Source=System
  ErrorCode=-2147467259
  NativeErrorCode=5
  StackTrace:
       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()
  InnerException:

Which process are you trying to get the filepath for (the value of name in line 35 of Form1.cs)?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.