0
Process[] processlist = Process.GetProcesses();
foreach (Process proc in processlist) {
              Console.Write(proc.MainModule.FileName);
}

it give an Win32Exception was unhandled access denied how to fix this? I tried run it as adm but it get same error. Thanks in advance!

Jack
  • 16,276
  • 55
  • 159
  • 284
  • 1
    Are you trying to get the name of a 64-bit process from a 32-bit app? – keyboardP Jan 09 '12 at 01:09
  • http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3aa75320-0524-4c74-ac3b-2d8aca319c51/ – Mohamed Abed Jan 09 '12 at 01:12
  • possible duplicate of [Process.MainModule --> "Access is denied"](http://stackoverflow.com/questions/8431298/process-mainmodule-access-is-denied) – shf301 Jan 09 '12 at 03:05

1 Answers1

1

There are some processes that you aren't allowed to access even as administrator. You can trap the exception and keep going:

foreach (Process proc in processlist)
{
  try
  {
    Console.WriteLine(proc.MainModule.FileName);
  }
  catch (Win32Exception e)
  {
     Console.WriteLine(proc.ToString() + "  " + e.Message);
  }
}
Mike W
  • 1,276
  • 8
  • 10