I am trying to get list of running processes that uses .NET. I am using the question posted here to do that.
using (Process p = Process.GetProcessById(pid))
{
var modules = p.Modules.Cast<ProcessModule>().Where(
m => m.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));
if (modules.Any())
Console.WriteLine(string.Format("{0} used .NET", pid));
}
I am running this program as Administrator. However for some proecsses ( e.g., Software Protection process) I get following exception.
Unhandled Exception: System.ComponentModel.Win32Exception: Access is denied
at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.ProcessManager.GetModuleInfos(Int32 processId)
at System.Diagnostics.Process.get_Modules()
at ConsoleApplication1.Program.Main(String[] args) in c:\Code\ConsoleApplication1\ConsoleApplication1\Program.cs:line
What I am doing wrong here?