How to retrieve the module version identifier (MVID) of a .NET assembly using reflection in c#?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With