8

I can get file version of exe(its own version) in vb6 by using App.Major ,App.Minor , App.Revision etc. But how can i get the same in vb.net. I tried using following 3 methods.

Text1.Text = My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build & "." & My.Application.Info.Version.Revision

Text2.Text = Me.GetType.Assembly.GetName.Version.ToString()

Text3.Text = My.Application.Info.Version.ToString()

In all 3 cases it was returning assembly version(I checked it in bin folder where the exe created in a xp machine.In windows 8 i didnt see any option like assembly version when i see file properties)

By default both assembly and file versions are same.But when i changed it manually in project properties->applicationassembly information->File version i came to know my code is returning assembly version. So how can i get the file version? What is assembly and file vesrion?

8 Answers 8

4

Actually taking Shawns Answer and improving it a little bit you can retrieve the file version with one line of code

Dim FileVer As String = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion

or you can place it in a label

Me.Label1.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion

Way to go! VB.NET as simple as usual....

Sign up to request clarification or add additional context in comments.

Comments

4

Use FileVersionInfo.GetVersionInfo, for example:

Dim myFileVersionInfo As FileVersionInfo =
    FileVersionInfo.GetVersionInfo([Assembly].GetExecutingAssembly().Location)

Comments

3

so in .NET programs, there are really two versions, not just one (of which i'm aware). the Assembly Version is what you are grabbing and is easier to retrieve in .NET. It is essentially ".NET's version" so when Assemblies have different versions, that's the one it uses.

then there is the "File Version" or in one place i see Microsoft has even called it "Assembly File Version" to make the confusion even worse. so one really needs to see whether the name includes "file" or not. this "File" version for an assembly is associated with the file system, so when you look at the version in Windows Explorer, this is what you'll get.

why Microsoft split those as two different things and didn't tie them together, i do not understand. maybe someone can enlighten me further.

i just figured out the code to grab the FileVersion. i found this question because i was looking for how to retrieve it too (in VB, not C#). I think C# makes most things easier, including accessing the Assembly. So here's the VB code to retrieve the File version of your program:

Dim fileName$ = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim fileName2$ = Application.ExecutablePath ' this grabs the filename too,
' but #2 ends with ".EXE" while the 1st ends with ".exe" in my Windows 7.

' either fileName or fileName2 works for me.
Dim fvi As FileVersionInfo = FileVersionInfo.GetVersionInfo(fileName)
' now this fvi has all the properties for the FileVersion information.
Dim fvAsString$ = fvi.FileVersion ' but other useful properties exist too.

wow, lots of code for something that should be something as simple as it was in VB6. Maybe even App.Version.ToString or something. wow. way to go, Microsoft! at least this one isn't as difficult as some of their stunts tho, like just playing your own music string.

3 Comments

This "File Version" is not part of the file system though, but a part of the file format. Windows explorer opens the file ad reads the version from the file contents, not from the file system.
@Zrin, i appreciate your correction. do you know where i can verify your correction to see for myself that this is indeed the case?
Search for "exe file version info" - EXE ("PE") files have version info fields and can also contain a "versioninfo" block. :NET assemblies have own embedded version info and much more meta information. "Managed code" ... If it would be a file system feature, than you would be able to set version info for any file, even a simplest text file.
1

The easiest way to get the application version is this:

    My.Application.Info.Version.Major
    My.Application.Info.Version.MajorRevision

etc.

Look at this article for a description of assemblies: http://visualbasic.about.com/od/FWTools/a/FWAssemblies.htm As a qucik info: An assembly can contain multiple file - so the assemblyversion is better than the file info.

3 Comments

incorrect. there's a big difference between Assembly Version and (Assembly) File Version.
When I run My.Application.Info.Version from a DLL I get the EXE's info. To get a DLL's "File Version", from within the DLL itself, I need Shawn's code... Something like... FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion
This also will not work for class libraries since the My.Application object does not exist.
0

The following is a more reliable way of getting the assembly's file version info from within the assembly itself:

Function GetAssemblyFileVersion() As String
    Dim ProjectAssembly As Reflection.Assembly 
    ProjectAssembly = Reflection.Assembly.GetExecutingAssembly()

    For Each attr As Attribute In ProjectAssembly.GetCustomAttributes(False)
        If TypeOf attr Is Reflection.AssemblyFileVersionAttribute Then
            Dim FileVerAttr As Reflection.AssemblyFileVersionAttribute = attr
            Return FileVerAttr.Version
        End If
    Next

    Throw New Reflection.TargetInvocationException(
        New KeyNotFoundException(
            "Cannot find custom attribute 'AssemblyFileVersionAttribute'"))
End Function

Comments

0

This is an extension method that I created to return the AssemblyFileVersion. This method will work for applications and class libraries.

''' <summary>
''' Gets the AssemblyFileVersion from the specified assembly.
''' </summary>
''' <param name="Assembly">[Assembly] Assembly to get the Assembly File Version from.</param>
''' <returns>[String] The Assembly's file version.</returns>
<Extension>
Friend Function GetFileVersion(ByVal Assembly As Assembly) As String
    On Error GoTo Err
    Return DirectCast(Assembly.GetCustomAttribute(GetType(AssemblyFileVersionAttribute)), AssemblyFileVersionAttribute).Version
Err:
    Return Nothing
End Function

Remember, Extension Methods need to be placed in a Module, they can not be created in a Class. If you want to learn more about Extension Methods type it in the search bar above. They will make your life easier. ;)

Make sure to include the following in your Module...

Imports System.Runtime.CompilerServices

Now on any assembly you can simply call something like this.

Dim Version as String = AnyAssembly.GetFileVersion()

Comments

0

I'm burned out but did you try Application.ProductVersion ? I don't see it in the other questions. This will return x.x.x.x whatever version is on the project settings. Note that for clickonce you need to get it from other site.

Comments

0
Dim verstr1 As String
        OpenFileDialog1.ShowDialog()
        Dim MyFileverInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(OpenFileDialog1.FileName)
        verstr1 = MyFileverInfo.FileName & vbCrLf & MyFileverInfo.InternalName _
            & vbCrLf & MyFileverInfo.CompanyName & vbCrLf & MyFileverInfo.FileMajorPart & vbCrLf & _
            MyFileverInfo.FileMinorPart

1 Comment

Consider giving a short explanation of your code so other can better understand your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.