FileVersion, not Assembly Version

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Is there a better way to Retrieve the "FileVersion" of my application in VB.Net other than using the WinAPI Call:
GetFileVersionInfo()

If not, how would I "Allocate" the Buffer to receive the data from that API DLL call.

Pascal Version would be done something like this:
VB.NET:
var Size: Integer;
var P: Pointer;
begin
  Size:=GetFileVersionInfoSize();
  P:=AllocMem(Size)
  GetFileVersionInfo(FileName, 0, Size, P^);
end;
From there I could use the VerQueryValue() or other such complicated API calls to retrieve the data, but in VB it is not so easy to manually allocate memory buffers for API calls.

I would definitely prefer a VB solution, and in all intents and purposes there should be a way for me to retrieve this information in VB.Net. I mean, in the Project Properties Page, under the Assembly Information, You can set the "File Version", so by all logical reasoning there should be a way to Get the same "File Version" when the EXE is running.

PS - Just so people know, this is NOT the information found in:
My.Application.Info.Version
that is the "Assembly Version", not the "File Version".

Thanks
 
This Got the file version and Not the assembly version for me.

VB.NET:
MessageBox.Show(Diagnostics.FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion)
 
This Got the file version and Not the assembly version for me.

VB.NET:
MessageBox.Show(Diagnostics.FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion)

Ahh,

i ended up playing with Reflection again and wrote this:
VB.NET:
Private _info As Microsoft.VisualBasic.ApplicationServices.AssemblyInfo = Nothing

      Dim FileVersion As String = ""
      Dim ass As Reflection.Assembly = Reflection.Assembly.Load(_info.AssemblyName)
      Dim ver() As Object = ass.GetCustomAttributes(False)
      For i As Integer = 0 To ver.Length
         If TypeOf ver(i) Is Reflection.AssemblyFileVersionAttribute Then
            FileVersion = DirectCast(ver(i), Reflection.AssemblyFileVersionAttribute).Version
            Exit For
         End If
      Next

This allowed me to create my generic "AboutBox" with only the passed parameter My.Application.Info, which is assigned too the _info private field before the above code is executed.

*shrug*,

I may end up changing that, but thanks for the pointer to the GetVersionInfo...why it is in Diagnostics instead of right in the AssemblyInfo seems a bit...odd.

Thanks
 
Add an extension

Thanks for the reflection Code!

I created this function as an extension in one of my modules and now I can call your function this way!
'================
Dim AppFileVersion as string = My.Application.Info.GetFileVersion()
'================
Here's the function
<System.Runtime.CompilerServices.Extension()> _
Function GetFileVersion(_info As Microsoft.VisualBasic.ApplicationServices.AssemblyInfo) As String
Dim FileVersion As String = ""
Dim Ass As Reflection.Assembly = Reflection.Assembly.Load(_info.AssemblyName)
Dim ver() As Object = Ass.GetCustomAttributes(False)
For i As Integer = 0 To ver.Length
If TypeOf ver(i) Is Reflection.AssemblyFileVersionAttribute Then
FileVersion = DirectCast(ver(i), Reflection.AssemblyFileVersionAttribute).Version
Exit For
End If
Next
Return FileVersion
End Function

Ahh,

i ended up playing with Reflection again and wrote this:
VB.NET:
Private _info As Microsoft.VisualBasic.ApplicationServices.AssemblyInfo = Nothing

      Dim FileVersion As String = ""
      Dim ass As Reflection.Assembly = Reflection.Assembly.Load(_info.AssemblyName)
      Dim ver() As Object = ass.GetCustomAttributes(False)
      For i As Integer = 0 To ver.Length
         If TypeOf ver(i) Is Reflection.AssemblyFileVersionAttribute Then
            FileVersion = DirectCast(ver(i), Reflection.AssemblyFileVersionAttribute).Version
            Exit For
         End If
      Next

This allowed me to create my generic "AboutBox" with only the passed parameter My.Application.Info, which is assigned too the _info private field before the above code is executed.

*shrug*,

I may end up changing that, but thanks for the pointer to the GetVersionInfo...why it is in Diagnostics instead of right in the AssemblyInfo seems a bit...odd.

Thanks
 
Back
Top