Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a Process is Managed in C#?

Tags:

c#

process

A bit of searching returns this result: Which processes are running managed code and which version?

However I am wondering if there is a 'better' way then simply iterating though the loaded modules? It seems a little quirky to look for the string "mscorwks.dll". Reading though the Process Class on MSDN doesn't seem to point out an obvious solution.

Assumptions Made

  1. .NET 4.0
  2. I have a "Process" in hand

Thank you

like image 975
aolszowka Avatar asked Feb 14 '11 22:02

aolszowka


2 Answers

For any future Googlers: I ended up using the suggested answer posted here How to check if a program is using .NET? (thanks 0xA3!)

Process mProcess = //Get Your Process Here
foreach (ProcessModule pm in mProcess.Modules)
{
    if (pm.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase))
    {
        return true;
    }
}

As an aside looking for "mscorwks.dll" as mentioned in my original post does not work for .NET 4.0.

like image 92
aolszowka Avatar answered Oct 26 '22 22:10

aolszowka


In code, get the full path of the executing process. Try to use Assembly.Load on the process. If it works, it's a .Net assembly :)

like image 32
Steve B Avatar answered Oct 26 '22 22:10

Steve B