12.13. 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 in situations such as:
Trying to determine if it has all of the "right" pieces present to execute.
Deciding on an assembly to dynamically load through reflection.
Looking for the newest version of a file or .dll from many files 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
{
Same = 0,
Newer = 1, // File1 is newer than File2
Older = 2, // File1 is older than File2
Error = 3
}The code for the CompareFileVersions method is shown in Example 12-3.
Example 12-3. CompareFileVersions method
private static FileComparison ComparePart(int p1, int p2) { return p1 > p2 ? FileComparison.Newer : (p1 < p2 ? FileComparison.Older : FileComparison.Same ); } public static FileComparison CompareFileVersions(string file1, string file2) { FileComparison retValue = FileComparison.Error; FileVersionInfo file1Version = FileVersionInfo.GetVersionInfo(file1); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access