How to determine if the assembly was ngen'd?

How can you determine if a particular .Net assembly has already been completed or not? I need to check the code. Even when invoking the command line would be nice. At the moment, I see no way to determine this.

+6
ngen
source share
2 answers

You can try to find your assembly in the "ngen cache" (C: \ Windows \ assembly \ NativeImages_v2XXXXXXX).

Bonded assemblies will have the following format name: [Basic]. n . [Baseextension].

+3
source share

Check code

Make sure we upload our own image for the running assembly. I am looking for the template "\ assemblyname.ni" in the loaded module file name.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Diagnostics; namespace MyTestsApp { class Program { static bool Main(string[] args) { Process process = Process.GetCurrentProcess(); ProcessModule[] modules = new ProcessModule[process.Modules.Count]; process.Modules.CopyTo(modules,0); var niQuery = from m in modules where m.FileName.Contains("\\"+process.ProcessName+".ni") select m.FileName; bool ni = niQuery.Count()>0 ?true:false; if (ni) { Console.WriteLine("Native Image: "+niQuery.ElementAt(0)); } else { Console.WriteLine("IL Image: " + process.MainModule.FileName); } return ni; } } } 

Command line solution:

Run "ngen display" on the command line.

Example:

ngen show MyTestsApp.exe

If set, it prints something like Native Images: MyTestsApp, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null

and returns 0 (% errorlevel%)

Otherwise, it prints:

Error: The specified assembly is not installed.

and returns -1

+4
source share

All Articles