FileVersionInfo.FileVersion returns ProductVersion

2020-02-16 02:22发布

I am trying to get the file version using C#:

string file = @"C:\somefile.dll";
Console.WriteLine(FileVersionInfo.GetVersionInfo(file).FileVersion);

For most files this is fine, however for some i receive results that are different than the ones presented in the Windows file explorer.

See the attached image: the file version presented in windows is "0.0.0.0", however the one i get using FileVersion property is "000.000.000.000".

I've tried using different versions of the .NET (2, 3.5, 4) which give the same results.

Anyone else experienced this issue?

Thanks Lior

enter image description here

2条回答
Emotional °昔
2楼-- · 2020-02-16 02:42

The reason is, in WIN32 API (and the file metadata), product versions are defined as string but file versions are defined as integer while in .NET, all of them are defined as integer.

If you use reflector and inspect FileVersionInfo class, you can see that they are loaded differently:

 this.productVersion = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, format, new object[] { codepage, "ProductVersion" }))

But:

this.fileMajor = HIWORD(fixedFileInfo.dwFileVersionMS);
this.fileMinor = LOWORD(fixedFileInfo.dwFileVersionMS);
this.fileBuild = HIWORD(fixedFileInfo.dwFileVersionLS);
this.filePrivate = LOWORD(fixedFileInfo.dwFileVersionLS);
查看更多
在下西门庆
3楼-- · 2020-02-16 03:07

It seems Windows Explorer are stripping leading 0s of the version parts.

Try creating an assembly with FileVersion 001.001.001.001, it will show as 1.1.1.1 in explorer. But your code would return the actual value (001.001.001.001).

EDIT:

Explorer will return 001.001.001.001 as ProductVersion, but only if AssemblyInformationalVersion isn't set, in which case it would return that as ProductVersion.

查看更多
登录 后发表回答