C# – List all referenced assemblies with Mono.Cecil

Mono.Cecil is a powerful open-source library for reading and writing .NET assembly. I used for it for many tools for editing assembly. This library is written and maintained by Jb Evain and is being rewritten with new model therefore some old codes do not work anymore. For example the AssemblyFactory is not supported anymore. If you want to find more about this library, you can browse its code at following link http://github.com/jbevain/cecil.

I often use Mono.Cecil to edit IL code so that I can rewrite and deobfuscate the code. Today I read a thread on mycsharp.de asking how to list all referenced assemblies so I make a small code snippet to solve this question.

AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly(@"D:\Synchronize\My Dropbox\Applications\Sudoku\Auto Fill In.exe");
foreach (AssemblyNameReference anrRef in asmDef.MainModule.AssemblyReferences)
	Console.WriteLine(anrRef.FullName + Environment.NewLine);

The code snippet is very simple. I just load the assembly into AssemblyDefinition and read the AssemblyReferences from the MainModule then write them out.

All referenced assemblies

However using this method I can not decide which type of referenced assembly (.exe or .dll) and where the referenced assemblies locate.

Leave a comment

Your email address will not be published. Required fields are marked *