Process question

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

Given a Process, is there a way to get the full path name of the executable?
 
Assuming you are the process, the following line should return the absolute
path to the exe:

System.Reflection.Assembly.GetEntryAssembly().Location
 
I am not the process. I was just playing with the Process class and while
iterating over the running psocesses, I just wondered if I could find the
originating file somehow. It would give me a method to keep track of
date/size changes, whatever, should the need ever arise.
 
This will do it. However, when it hits process name 'wmiapsrv', it throws
this exception:

System.InvalidOperationException

procs=Process.GetProcesses();
....
Console.WriteLine(" MainModule: {0}", procs[0].MainMod
 
....the following code enumerates the running processes and its
corresponding path.

Process[] oLocalProcesses = Process.GetProcesses();
string ProcessName = string.Empty;
string ProcessPath = string.Empty;

foreach (Process oProcess in oLocalProcesses) {
ProcessName = oProcess.ProcessName;

try {
ProcessPath = oProcess.MainModule.FileName;
}
catch {
ProcessPath = "N/A";
}

Console.WriteLine("{0} {1}", ProcessName, ProcessPath);
}

Console.ReadLine();

HTH, Metallikanz!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top