Get file version

I can get the exe file version (its own version) in vb6 using App.Major , App.Minor , App.Revision , etc. But how can I get the same in vb.net. I tried using the 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 three cases, it returned the assembly version (I checked it in the bin folder where exe was created in the xp machine. In Windows 8, I did not see any option, such as the assembly version, when I see the property file)

By default, both build and file versions are the same. But when I changed it manually in project properties->applicationassembly information->File version , I found out that my code returns the version of the assembly. So how can I get the file version? What is assembly and vesrion file?

+6
source share
7 answers

so there are two versions of .NET programs, not just one (which I know of). Assembly version is what you capture and it is easier to retrieve in .NET. This is essentially a ".NET version", so when assemblies have different versions, this is the one it uses.

that is, "File Version" or in one place, I see that Microsoft even called it "Build File Version" to make the confusion even worse. so you really need to find out if the name includes "file" or not. this version of the โ€œFileโ€ for the assembly is related to the file system, so when you look at the version in Windows Explorer, this is what you get.

why Microsoft divided them into two different things and did not bind them together, I do not understand. maybe someone can enlighten me further.

I just figured out the code to capture FileVersion. I found this question because I was looking for how to get it (in VB, not C #). I think C # simplifies most of the things, including access to the Assembly. So, here is the VB code to extract the version of your program file:

 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, a lot of code for something that should be something simple, like it was in VB6. Maybe even App.Version.ToString or something like that. Wow. way, Microsoft! at least itโ€™s not as difficult as some of their tricks, for example, just playing your own musical string.

+3
source

Use FileVersionInfo.GetVersionInfo , for example:

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

Actually, taking Shawn's Answer and improving it a little, you can get a version of the file with one line of code

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

or you can put it in a shortcut

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

How to go! VB.NET is as simple as usual ....

+2
source

The easiest way to get the application version:

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

and etc.

Take a look at this article for assembly descriptions: http://visualbasic.about.com/od/FWTools/a/FWAssemblies.htm Like qucik info: An assembly can contain multiple files, so an assembly is better than file information.

+1
source

The following is a more reliable way to get the version information of the assembly file from 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 
0
source

This is the extension method that I created to return 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 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 that Extension Methods must be placed in Module ; they cannot be created in Class . If you want to know more about Extension Methods , enter it in the search bar above. They will make your life easier .;)

Be sure to include the following in your module ...

 Imports System.Runtime.CompilerServices 

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

 Dim Version as String = AnyAssembly.GetFileVersion() 
0
source

I burned out, but you tried Application.ProductVersion ? I do not see this in other matters. This will return xxxx any version in the project settings. Please note that for clickonce you need to get it from another site.

0
source

All Articles