0

My question is not same as below.

Compare Two DLL's

I am trying to compare whether 2 DLLs are equal binaries or not using C# code. If needed I can refer some third party DLL but the comparision must be done by C# code not manual opening tool.

This is my bigger task.. C# Common libraries same location for different WCF services

I have a dll called MyDll.dll at below locations

C:\Source\MyDll.dll

C:\Destination\MyDll.dll

I wrote a method which gets MyDll.dll from C:\Source and drop/replace into C:\Destination but I do not want to blindly replace MyDll.dll in C:\Destination. I want to check whether C:\Source\MyDll.dll and C:\Destination\MyDll.dll are same or not. If not then only replace.

Please remember everything needs to be happening in C# code since this method runs on a start event of windows service.

public void LoadAssembly()
{
  string source = @"C:\Source\MyDll.dl"
  string destination = @"C:\Destination\MyDll.dll"

  // To copy a file to another location and
  // overwrite the destination file if it already exists.
  System.IO.File.Copy(source, destination, true);

}

Looked at this not use C# - comparing two .net dlls using reflection

UPDATE

I created below method and I feel it has performance issue depending on how big my dll is. Is there any way I can improve this.

public static bool AreFilesEqual()
{
    string source = @"C:\Source\MyDll.dll";
    string dest = @"C:\Destination\MyDll.dll";

    byte[] sourceFileBytes = File.ReadAllBytes(source);
    byte[] destinationFileBytes = File.ReadAllBytes(dest);

    // if two files length are not same then they are not equal
    if(sourceFileBytes.Length != destinationFileBytes.Length)
    {
        return false;
    }

    return sourceFileBytes.SequenceEqual(destinationFileBytes);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ziggler
  • 3,361
  • 3
  • 43
  • 61
  • 2
    So why not just use a checksum? – stuartd Feb 04 '20 at 22:24
  • 3
    Does this help? https://stackoverflow.com/questions/968935 – D Stanley Feb 04 '20 at 22:25
  • What's your definition of "equal"? Completely equal binaries? Equal assembly name and version? – D Stanley Feb 04 '20 at 22:26
  • @D Stanley.. Thanks.. look positive.. let me look into it and get back – Ziggler Feb 04 '20 at 22:27
  • What's your definition of "equal"?.. Equal binaries... – Ziggler Feb 04 '20 at 22:29
  • 4
    Your rule is "replace only if different", but if they are the same then "replacing" is the same as not replacing, right? Are you attempting to avoid the unnecessary replacement for *performance* reasons or do you have another reason to avoid the unnecessary replacement? – Eric Lippert Feb 04 '20 at 22:41
  • Yes we have so many libraries which we are trying to read from common folder. We want to replace only changes for performance issue and also keep track of what libraries are drop when... we want to preserve date modified.. so replace only when library changes else no replace... – Ziggler Feb 04 '20 at 22:44
  • In your case, I think you just need to compare the two files and do the replace if they aren't identical. Check the [memcmp](http://pinvoke.net/default.aspx/msvcrt/memcmp.html?diff=y) function. –  Feb 04 '20 at 22:54
  • @JQSOFT.. thats what I am trying to do... but not just blindly check library size I want to check contents also.. looking at memcmp.. – Ziggler Feb 04 '20 at 22:54
  • 3
    Is there a reason not to use library version numbers for this? Build tools are quite good at autoincrementing version numbers on a rebuild and only rebuilding when something has actually changed, so you move the problem somewhere more appropriate. Also you avoid a massive byte-by-byte comparison of all your libraries every time your service starts. – Rich N Feb 04 '20 at 23:48
  • The libraries I am comparing are all in-house and all teams says they never increment versions. Thats the way it is... – Ziggler Feb 06 '20 at 00:30

0 Answers0