is it possible to tell if an assembly has changed?
I have a standard project that generates an assembly called MyAssembly.dll.
In a separate project i read the assembly and generate a hash.
When i generate a hash for the assembly it is different each time i recompile. I have set the assembly version to be static, are there any other attributes that i need to change?
class Program
{
static void Main(string[] args)
{
var array = File.ReadAllBytes(@"MyAssembly.dll");
SHA256Managed algo = new SHA256Managed();
var hash = algo.ComputeHash(array);
Console.WriteLine(Convert.ToBase64String(hash));
}
}
Thanks
Rohan
To look at what is on the machine click Start– and type in the path to the assembly folder which is C:windowsassembly and press ENTER. This will bring up a folder that shows a list of installed components. Simply Right-Click on the one you want to check and select properties.
If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.
An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.
How to manually determine if a file is an assembly. Start the Ildasm.exe (IL Disassembler). Load the file you want to test. If ILDASM reports that the file is not a portable executable (PE) file, then it is not an assembly.
You are probably going to need to use the version number attribute. A hash will not work because any time you recompile an assembly, it's going to be different -- even if the code didn't change at all. The reason is that every time you compile, the compiler embeds a guid into the assembly, and it puts the same guid into the corresponding .pdb file. The guid will change every time the assembly is compiled.
This is how the debugger matches an assembly to the correct version of its .pdb file (it's also why you have to always save the .pdb's on anything you release, and you cannot rely upon being able to generate a pdb to match an existing assembly)
Every assembly has a ModuleVersionId
GUID that works like a hash. If you change a single character, module id changes, but if you revert it you get the old id back. It's useful for comparing two versions of an assembly.
var assembly = Assembly.GetEntryAssembly();
var hashId = assembly.ManifestModule.ModuleVersionId;
Console.WriteLine(hashId);
40744db8-a8fe-4591-9b2c-d9e3e04a2f0a
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.module.moduleversionid?view=net-5.0
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