Timo Sperisen
Timo Sperisen

Reputation: 483

Get .NET Assemblies Info on process (like Sysinternals Process Explorer) via Code (C# or PowerShell)

In Sysinternals Process Explorer there exists a tab ".NET Assemblies".

This tab is only shown in the properties for processes that actually use .NET Assemblies.

How can I get the same information on any running process using PowerShell or C#?

Thanks.

Upvotes: 2

Views: 2632

Answers (2)

Renat
Renat

Reputation: 8992

ClrMD (Microsoft.Diagnostics.Runtime) could be used.

An example of Powershell usage:

  • download Microsoft.Diagnostics.Runtime nupkg file and unpack it to get Microsoft.Diagnostics.Runtime.dll

  • get an ID of the target process

  • run script below using the process ID and correct path to Microsoft.Diagnostics.Runtime.dll

     [int]$targetProcessId=12345
     [Reflection.Assembly]::LoadFile('.\Microsoft.Diagnostics.Runtime.dll')
    
     $dataTarget = [Microsoft.Diagnostics.Runtime.DataTarget]::AttachToProcess($targetProcessId, 1) #AttachFlags.Noninvasive
     try
     {
         $clrRuntime = $dataTarget.ClrVersions[0].CreateRuntime()
    
         foreach ($domain in $clrRuntime.AppDomains) {
             Write-Host "Domain ID: " $domain.Id ", Name: " $domain.Name
    
             foreach ($clrModule in $domain.Modules) {           
                 Write-Host "`t`t" $clrModule.Name
             }
         }
     }
     finally
     {
         $dataTarget.Dispose()
     }
    

Upvotes: 1

3615
3615

Reputation: 3875

After looking at this answer I've realized that maybe there is no easy way to get what you need. So let's go with MDBG to solve your challenge for managed processes:

        _engine = new MDbgEngine();
        _engine.Attach(p.Id, RuntimeEnvironment.GetSystemVersion());
        _engine.Processes.Active.Go().WaitOne();
        foreach (MDbgAppDomain appDomain in _engine.Processes.Active.AppDomains) {
            foreach (CorAssembly assembly in appDomain.CorAppDomain.Assemblies) {
                Console.WriteLine(assembly.Name);
            }

        }

You will have to using MDBG package from nuget: <package id="Microsoft.Samples.Debugging.MdbgEngine" version="1.4.0.0" targetFramework="net452" />

Upvotes: 2

Related Questions