Compares the contents of two files with an optional message digest.
A boolean value which specifies if the files are identical using the criteria specified by the comparison options.
The CompareFile method can be used to either compare the contents of two files or compare the contents of a file against a pre-computed hash value to determine if they match. In addition to comparing file contents, the method can also check the file creation and/or modification times to ensure they are the same.
In most cases, an application will specify either two files to compare or a message digest value. If you call the method using two file names and a message digest, the contents of both files must be identical and the computed hash value must match the specified message digest. If any of the requirements are are not satisfied, the method will return False. For example, if the actual contents of the two files are identical, but the value specified by fileDigest does not match the computed hash value of files, the method will still return False.
It is permitted to use environment variables in a file name by surrounding the variable name with the percent symbol. If the file name contains any leading or trailing space characters after the variable expansion, they will be removed. The file must exist and the current process must have permission to read the contents of the file.
This method performs the comparison in a way to minimize potential disk I/O whenever possible. First, the size of the two files are compared and if they are different, then no further checks are performed. Next, if either of the file time comparison options have been specified, the file timestamps are compared and no further checks are performed if they do not match. A byte comparison of both files will only be performed after the other comparison criteria has been satisfied.
Comparing the contents of a file against a message digest value is the most expensive operation in terms of memory and processor utilization. The larger the files are, and the more complex the hash algorithm, the more compute-intensive the operation will be. In most cases, the SHA-1 algorithm provides a good balance between resource utilization and minimizing the possibility of hash collisions where different files could potentially generate the same hash value.
If it is critically important to minimize the possibility of a hash collision, use the SHA-256 algorithm; however, be aware of the impact it can have on performance when used with very large files. The current thread will block while the hash is being computed and if a large amount of data is being processed, this may cause the application to become non-responsive. In that case, it is recommended you create a background worker thread and call this method from within that worker thread, rather than inside the main UI thread.
To avoid potential problems with relative pathing in multithreaded applications, it is recommended you use absolute paths whenever possible. If you do not specify an absolute path, the file locations will be considered relative to the current working directory for the process. Because this is shared between all threads, it is possible for one thread to change the current working directory at any time and this can create inconsistent results. If you must use relative paths in a multithreaded application, use a mutex or critical section to ensure other threads cannot change the current working directory for the process while performing the file comparison.
This method will normalize the file names and perform checks to make sure the names do not contain illegal characters. If the name includes quotes, wildcard characters or other invalid symbols the method will fail and with the last error code set to errorInvalidFileName. It is permitted to use forward slashes in path names and these will be converted to the standard backslash character used with Windows paths. This method cannot be used to compare the contents of NTFS alternate data streams.
FileEncoder Class | SocketTools Namespace | FileEncoder.CompareFile Overload List | CompressFile | ExpandFile