O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.25. Comparing Version Information of Two Executable Modules

Problem

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.

Solution

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required