1

Is there any method to display all currently running process full path and save in variable or list in C#?

I use "tasklist". But it only display process name and ID and nothing else... and also I tried Process.GetProcesses(), but I'munable to fetch full path.

Process[] processlist = Process.GetProcesses();
Console.WriteLine(processlist);
foreach (Process theprocess in processlist)
{
  Console.WriteLine("Process: {0}\t\t\t\t\t\t ID: {1}", theprocess.ProcessName, theprocess.Id);
  Console.WriteLine(theprocess);
}
Console.ReadKey()

I expect full path of running process like C:\WINDOWS\system32\example.exe if example.exe is running on that time.

Blue
  • 22,608
  • 7
  • 62
  • 92
pranz
  • 11
  • 1

1 Answers1

2

You want MainModule.FileName located on the Process class:

string fullPath = theprocess.MainModule.FileName;
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Note to all and sundry, `MainModule` is likely to throw an exception in a good subset of circumstances and needs to be used with the appropriate fault tolerance. best read the documentation – TheGeneral Oct 22 '19 at 05:18