How to retrieve the module version identifier (MVID) of a .NET assembly using reflection in c#?
Asked
Active
Viewed 1,781 times
8
-
see if it helps - http://stackoverflow.com/questions/2940857/determine-whether-net-assemblies-were-built-from-the-same-source – Kumar Mar 03 '11 at 15:44
2 Answers
12
Should be:
var myAssembly = Assembly.GetExecutingAssembly(); //or whatever
var mvid = myAssembly.ManifestModule.ModuleVersionID;
There can be other modules in an assembly, but the ManifestModule would be the one that "identifies" the assembly itself.

KeithS
- 70,210
- 21
- 112
- 164
5
Here's a sample that doesn't use Reflection to load the assembly but instead uses System.Reflection.Metadata:
using (var stream = File.OpenRead(filePath))
{
PEReader reader = new PEReader(stream);
var metadataReader = reader.GetMetadataReader();
var mvidHandle = metadataReader.GetModuleDefinition().Mvid;
var mvid = metadataReader.GetGuid(mvidHandle);
}
And here's a sample of using Mono.Cecil:
var module = Mono.Cecil.ModuleDefinition.ReadModule(filePath);
var mvid = module.Mvid;
And here's an example of a standalone code to read the MVID without any dependencies. It is a stripped-down version of Mono.Cecil in a single file: https://github.com/KirillOsenkov/MetadataTools/blob/master/src/PEFile/ImageReader.cs

Kirill Osenkov
- 8,786
- 2
- 33
- 37