1

I am trying to get the file version using .net core.

Method1

string file = @"C:\somefile.dll";
Console.WriteLine(AssemblyName.GetAssemblyName(file).Version.ToString());

Problem in Method1 is if file is not assembly it throws BadImageFormatException

Method2

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

Problem in above code is it returns result different than ones presented in the windows file explorer.

See the attached image.
The file version presented in windows explorer is "2.2.27414.6",
however the one i get using FileVersion property is "2.2.27414.06 @BuiltBy: dlab14-DDVSOWINAGE021 @Branch: release/2.2 @SrcCode: https://github.com/dotnet/core-setup/tree/6b8ad509b6a48bcca07e8c413d6ffed8988547cc. Commit Hash: 6b8ad509b6a48bcca07e8c413d6ffed8988547cc".

windows explorer view

RPrashant
  • 217
  • 3
  • 13
  • 1
    Possible duplicate of [How can I get the assembly file version](https://stackoverflow.com/questions/909555/how-can-i-get-the-assembly-file-version) – Anurag R Aug 06 '19 at 04:59
  • @AnuragR Thanks for the help but please read the question carefully. – RPrashant Aug 06 '19 at 05:03
  • @RPrashant. I'd suggest reading **all** of the answers in the duplicate (not just one of them). I strongly suspect one of them does what you want. – mjwills Aug 06 '19 at 05:47
  • As per Jeffrey Richter’s “CLR via C#”: “Unfortunately, the Windows Explorer Properties dialog box is missing entries for some of the attributes. In particular, it would be great if the value of the AssemblyVersion attribute were shown because the CLR uses this value when loading assemblies”. So if you’re after the real assembly version (when targeting assemblies), what Windows explorer shows is not the real value, unless of course AssemblyVersion and AssemblyFileVersion are set to the same value. – Mihai Albert Aug 06 '19 at 21:31

1 Answers1

1

I did some research. Windows explorer and the file properties dialog do not list the file version string, they format the file version information by manually putting the file version parts together. When I modified the AssemblyFileVersion field to include some bogus information e.g. [assembly: AssemblyFileVersion("1.0.0.0 @asdfasdf asdf asdf asdf")], windows explorer and the file properties dialog box just listed the file version as you indicate above. But when I debug and look at the FileVersion field, the extra bogus information is there.

To answer your question, it appears that you will need to also create the file version by manually putting the file version parts together manually (i.e. FileMajorPart.FileMinorPart.FileBuildPart.FilePrivatePart)

Jeff
  • 646
  • 1
  • 7
  • 13