Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an assembly has changed

Tags:

c#

.net

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

like image 987
Rohan West Avatar asked Jun 30 '09 10:06

Rohan West


People also ask

How do I find the assembly version?

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.

How do I find the assembly version of a DLL?

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.

Is a DLL file an assembly?

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 can I tell if a DLL is .NET assembly?

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.


2 Answers

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)

like image 168
JMarsch Avatar answered Sep 28 '22 21:09

JMarsch


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

like image 26
abdusco Avatar answered Sep 28 '22 21:09

abdusco