How to get path of a running process?
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
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
So assuming you have an array of all processes, in your foreach
foreach(Process p in processes)
{
string path = p.MainModule.FileName;
}
makman99
Junior Poster in Training
68 posts since Jan 2010
Reputation Points: 12
Solved Threads: 13
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:
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
Which process are you trying to get the filepath for (the value of name in line 35 of Form1.cs)?
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
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
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
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
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124