Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
Use FileVersionInfo.GetVersionInfo
, for example:
Dim myFileVersionInfo As FileVersionInfo =
FileVersionInfo.GetVersionInfo([Assembly].GetExecutingAssembly().Location)
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.
–
–
–
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....
The easiest way to get the application version is this:
My.Application.Info.Version.Major
My.Application.Info.Version.MajorRevision
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.
–
–
–
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
Throw New Reflection.TargetInvocationException(
New KeyNotFoundException(
"Cannot find custom attribute 'AssemblyFileVersionAttribute'"))
End Function
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
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()
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
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.