Saturday, November 18, 2006 9:08 AM bart

Which processes are running managed code and which version? (C# code snippet)

A question on the discussion groups a couple of weeks ago: how to find out which processes are running managed code, and which version of the CLR? Here's the answer:

using System; using System.ComponentModel; using System.Diagnostics; class Proc { public static void Main() { Console.WriteLine("{0,-20} {1}", "Process Name ", ".NET version"); Console.WriteLine("{0,-20} {1}", "--------------------", "-------------"); foreach (Process p in Process.GetProcesses()) try { foreach (ProcessModule m in p.Modules) if (m.ModuleName.ToLower() == "mscorwks.dll") Console.WriteLine("{0,-20} {1}", p.ProcessName, m.FileVersionInfo.ProductVersion); } catch (Win32Exception) { } } }

On my machine it prints:

Process Name .NET version -------------------- ------------- WINWORD 2.0.50727.112 OUTLOOK 2.0.50727.112 dexplore 2.0.50727.112 WindowsLiveWriter 2.0.50727.112 devenv 2.0.50727.112 iexplore 2.0.50727.112 proc 2.0.50727.112

Notice the code can be improved quite a bit, as other exceptions might arise (InvalidOperationException when a process has exited during iteration; yes, this happens sometimes). So I only caught Win32Exception (lots of Access Denied errors can occur, especially on Windows Vista) to avoid the criticism "do never catch all" (which might be appropriate in here).

The cmdline equivalent of this is:

C:\temp>tasklist /FI "MODULES eq mscoree.dll" Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ iexplore.exe 4748 Console 1 42.092 K OUTLOOK.EXE 1524 Console 1 94.012 K devenv.exe 1424 Console 1 157.600 K WINWORD.EXE 7476 Console 1 44.288 K dexplore.exe 9376 Console 1 66.300 K WindowsLiveWriter.exe 11728 Console 1 48.128 K

or

C:\temp>tasklist /M mscoree.dll Image Name PID Modules ========================= ======== ============================================ iexplore.exe 4748 mscoree.dll OUTLOOK.EXE 1524 mscoree.dll devenv.exe 1424 mscoree.dll WINWORD.EXE 7476 mscoree.dll dexplore.exe 9376 mscoree.dll WindowsLiveWriter.exe 11728 mscoree.dll

Enjoy!

kick it on DotNetKicks.com

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# Myambutol

Tuesday, October 05, 2010 12:08 AM by Myambutol

Myambutol

nbauhzpnembnfqliupteskkjioizmvgkfanewxbbqgjhutfabhguzhwklwdvqzkrpczbqhcxlnweuwfhtimowroxodnfxqlkdxcsgxxtcwe

#

Thursday, October 07, 2010 7:11 AM by TrackBack

. . . .