How to determine if assembly has been ngen'd?
Asked Answered
U

2

7

How can you determine whether a particular .Net assembly has already been ngen'd or not? I need to check from code. Even invoking the command-line would be fine. At the moment I can't see any way of determining this.

Unhesitating answered 30/1, 2010 at 9:18 Comment(1)
In code? From the command line? Interactively? What? It has to be in the file properties somewhere...Terhune
E
4

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

Сached assemblies will have the following format name: [basename].ni.[baseextension].

Exarate answered 14/2, 2010 at 15:41 Comment(3)
Windows 7 seems to store them is a slightly different location, and with no '.ni.' in the name: C:\Windows\assemblyUnhesitating
Very strange. I have Windows 7 installed, and "ngen cash" path is "C:\Windows\assembly\NativeImages_v2.0.50727_32\".Exarate
I have Win7 and you cannot navigate to those folders using the Windows Explorer. You should use cmd and dir inside the assembly folder.Mcclendon
T
7

Check From Code

Check if we are loading an native image for the executing assembly. I am looking for the pattern "\assemblyname.ni" in loaded module filename property.

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 command prompt.

Example:

ngen display MyTestsApp.exe

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

and returns 0 (%errorlevel%)

Otherwise, it prints out:

Error: The specified assembly is not installed.

and returns -1

Thurston answered 15/12, 2013 at 9:54 Comment(1)
This is a good way of going about it, thanks for laying it out. The ngen documentation is missing a lot of the error codes that you should be expecting.Troposphere
E
4

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

Сached assemblies will have the following format name: [basename].ni.[baseextension].

Exarate answered 14/2, 2010 at 15:41 Comment(3)
Windows 7 seems to store them is a slightly different location, and with no '.ni.' in the name: C:\Windows\assemblyUnhesitating
Very strange. I have Windows 7 installed, and "ngen cash" path is "C:\Windows\assembly\NativeImages_v2.0.50727_32\".Exarate
I have Win7 and you cannot navigate to those folders using the Windows Explorer. You should use cmd and dir inside the assembly folder.Mcclendon

© 2022 - 2024 — McMap. All rights reserved.