I have a C# solution, which references a dll I created from a different C# solution.
It is easy enough to determine my solution's product version with Application.ProductVersion. However, what I really need is a way to determine the file version of the exe and the dll separately, within my program. I want to display the dll and exe's file versions in my About dialog. What would the code look like to do this?
If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.
You can get file version of file using PowerShell Get-Command cmdlet. In the above command, Get-Command get dll assembly file version of the specified file using FileVersionInfo. FileVersion property.
The simplest way is if you know a type in the referenced assembly:
AssemblyName name = typeof(MyCompany.MyLibrary.SomeType).Assembly.GetName();
Assembly.GetName returns an AssemblyName which has a Version property indicating the version of the assembly.
Alternatively, you can get the assembly names of all assemblies referenced by the executing assembly (i.e., the .exe):
AssemblyName[] names = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
Perhaps the easiest solution is this:
var version = Assembly.GetAssembly(typeof(SomeType)).GetName().Version;
where SomeType is a type you know for sure that is defined in that particular assembly. You can then call .ToString() on this version object or look at its properties.
Of course, this will blow up in a huge fireball the moment you move your type into another assembly. If this is a possibility, you will need a more robust way to find the assembly object. Let me know if this is the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With