Process question

  • Thread starter Thread starter web1110
  • Start date Start date
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!
 
Back
Top