How to tell if some file is managed assembly or not?

Another commonly asked question.

There are many ways to detect if a given file is a managed assembly or not. For example, in VS.Net 2002/2003, dumpbin has an option /clrheader. It will dump the CLR header for you if the file is a managed assembly. You can also run ildasm on the file. If it is not an assembly, it will complain.Other people suggest to inspect the PE header of the file.

What we are talking about here is API level detection.

First unmanaged API.

In .Net framework 1.1 mscoree.dll has an API GetFileVersion. Its definition is

STDAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD* dwLength);

Suzanne described what this API does here( Basically it tells which runtime this assembly is built against. If the given file is not a managed assembly, this API returns 0x8007000B (ERROR_BAD_IMAGE).

Now managed API.

Assembly.LoadFrom will throw BadImageFormatException if the given file is not a managed assembly.

This exception could be thrown for other reason as well. Suzanne kindly points out a more robust way. Once you catch the BadImageFormatException, look at its HResult field. If HResult is COR_E_ASSEMBLYEXPECTED, it means this is not a managed assembly.

Comments (5)

  1. Correction. Mscoree!GetFileVersion is documented. See mscoree.h in SDK for its definition.

  2. Correction again. Suzanne said the HResult is COR_E_ASSEMBLYEXPECTED, not COR_E_BADIMAGEFORMAT.

  3. A couple of weeks ago a received an error report from a customer of my MZ-Tools add-in saying that VS