11.25. Comparing Version Information of Two Executable Modules


You need to programmatically compare the version information of two executable modules. An executable module is a file that contains executable code such as an EXE or DLL file. The ability to compare the version information of two executable modules can be very useful to an application if it is trying to determine if it has all of the “right” pieces present to execute or when deciding on an assembly to dynamically load through reflection. This trick is also useful when an application is looking for the newest version of a file or DLL from many files or DLLs spread out in the local filesystem or on a network.


Use the CompareFileVersions method to compare executable module version information. This method accepts two filenames, including their paths, as parameters. The version information of each module is retrieved and compared. This file returns a FileComparison enumeration, defined as follows:

public enum FileComparison
    Error = 0,
    File1IsNewer = 1,
    File2IsNewer = 2,
    FilesAreSame = 3

The code for the CompareFileVersions method is:

public FileComparison CompareFileVersions(string file1, string file2) { FileComparison retValue = FileComparison.Error; // do both files exist? if (!File.Exists(file1)) { Console.WriteLine(file1 + " does not exist"); } else if (!File.Exists(file2)) { Console.WriteLine(file2 + " does not exist"); } else { // get the version information FileVersionInfo file1Version = FileVersionInfo.GetVersionInfo(file1); ...

Get C# Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.