0

I have an application that checks for updates. To check for updates I need to get the version of the file on the user's computer. I used this code:

if (File.Exists(dataFile))
{
    var verLocal = Version.Parse(FileVersionInfo.GetVersionInfo(dataFile).FileVersion);
    if (verSite > verLocal)
    {
        needToAdd = true;
    }
}

Today I found out that the method FileVersionInfo.GetVersionInfo(String) may not get the file version! Here is a description from the help:

If the file did not contain version information, the FileVersionInfo contains only the name of the file requested.

So that there was no error, I did like this:

if (File.Exists(dataFile))
{
    if (Version.TryParse(FileVersionInfo.GetVersionInfo(dataFile).FileVersion, out var verLocal))
    {
        if (verSite > verLocal)
        {
            needToAdd = true;
        }
    }
}

But now there is a problem - if the user this method will never return the version of the file, then the user will never receive updates! So I need a way to get the version of the file that always works.

Are there alternatives to this method in c#?

  • What are you checking for updates to? Your own program? Other programs on the computer? Something else? – Herohtar Oct 17 '19 at 19:42
  • Possible duplicate of [How to get .exe file version number from file path](https://stackoverflow.com/questions/11350008/how-to-get-exe-file-version-number-from-file-path). Look at [this](https://stackoverflow.com/a/23325102/1797425) answer in that thread. – Trevor Oct 17 '19 at 19:58
  • @Çöđěxěŕ The fact of the matter is that I saw it. There is a suspicion that the version is not returned due to lack of access for the user – Александр Пекшев Oct 17 '19 at 20:13
  • 1
    If they're your own files, you can avoid the problem by simply making sure that you always include the version information. Or better yet, store the version information somewhere else instead of trying to pull it from the files themselves. – Herohtar Oct 17 '19 at 20:31
  • @Herohtar I myself already began to incline towards the idea of ​​writing versions in the registry and checking them there – Александр Пекшев Oct 18 '19 at 17:30

1 Answers1

-2

That Version info metadata really only applies the Executeables or DLL's. It is supposed to be set during compilation. I have not seen it apply (be written) to any word document, image or similar non-executeable file.

A pretty dated approach for archiving, would be the old Archive Bit/Atribute. Just throwing it out there for completeness.

Usually for a "did it change?" check, it is sufficient to just check the file Size and LastUpdated dates of the file for changes. If you pick any backup maker, it will not do more advanced checks then this (plus the archive bit thing as a option). That one of those two values does not change can happen. But both of them is to unlikely to bother with.

The only 100%* reliable way to check for changes is to calculate a files hash-value. But that usually is something you only do during stuff like install verifications, not as a basic backup.

*Technically not even that is 100%. Hash Colissions are a thing, but are realistically impossible if you already check size and change date.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • This question isn't about checking for file *changes*; it's about reading the existing file's *version* to compare against the most recently published version of the application to see if there is an update available. – Herohtar Oct 17 '19 at 20:35
  • @Herohtar The wording is "I have an application that checks for updates." The process to check if a file needs a update is identical to the backup one. Because Backup programms simply look if the file needs to be updated in the backup :) The programm might do some caching of some data like Hashes to avoid recomputation, but at the end of the day that is what every installer on this planet does. – Christopher Oct 17 '19 at 21:01
  • Not true. Checking for available updates is usually done by comparing the version number of the installed application against a version number provided elsewhere (the application's website, for example). It's also usually done against a single version number, not on a per-file basis. – Herohtar Oct 17 '19 at 21:04
  • @Herohtar That only works if there a) Is a version number. b) It is properly updated. Just checking file Size and Date has no such issues and works reliably for 100% of all programm, shared programm and other files. | "The more they overthink the plumbing, the easier it is to just stop up the drain." – Christopher Oct 17 '19 at 22:01
  • 1
    If you control the files (which is the case here, per comments from the OP) then you can ensure that there *is* a version number and you properly update it. The OP isn't looking to update arbitrary system files or other programs. Checking file date is an extremely unreliable method, because there are large number of things that can cause the file date to change. Checking file size is also unreliable because it is possible that your files are the same size, and it doesn't tell you which file is newer. – Herohtar Oct 17 '19 at 22:07