Ozone
Ozone

Reputation: 357

FileVersionInfo retrieving incorrect file version

I have to retrieve values under details tab e.g, File version, Product Version for .dll and .exe files through C#. I'm using the following code for this.

     FileVersionInfo myFile = FileVersionInfo.GetVersionInfo('Name of the file');
     //File Version
     string fileVersion = myFile.FileVersion;

The issue with this code is that it gives incorrect file version for some files. Details tab of these files shows different file version and code retrieves incorrect value. I'm not sure why this is happening.

Please help. Thanks in advance!!

Upvotes: 0

Views: 1596

Answers (2)

navule
navule

Reputation: 3624

You can get full application file version with the below approach

FileVersionInfo myFile = FileVersionInfo.GetVersionInfo('Name of the file');
string fileVersion = string.Format("{0}.{1}.{2}.{3}", myFile.FileMajorPart, 
                                                      myFile.FileMinorPart, 
                                                      myFile.FileBuildPart, 
                                                      myFile.FilePrivatePart)

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941337

expected version(from Explorer) is 10.0.0.35 and i got 10.0.000.0035

That's the same number. The file version number appears in the native resource twice. Something you can also see when you edit a version resource in a C++ program. There's a human readable version with no restrictions on the format. That's what you are reading, note how FileVersionInfo.FileVersion returns a string.

And there's a machine readable version, a 64-bit number. With 16-bits each for the 4 parts. Which is what Explorer is reading. The corresponding properties are FileMajorPart, FileMinorPart, FileBuildPart and FilePrivatePart. Note how they return an int.

ProductVersion has this too.

Upvotes: 1

Related Questions