Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3506
  • Last Modified:

File Properties -> Version Number

Hi there!

You know when you right-click a file on explorer, select properties and then check its version number on the "version" tab of the properties dialog?

Well, how can I do the same from my VB app? i.e., get a file's version number programmatically?

Note: I rather steer away from adding more dependencies to my app, a API-only solution would be a bless :-)

Thanks and have a nice day!
Claudio
0
ca_heckler
Asked:
ca_heckler
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
from the Project Properties, into the Make tab, you have the version number and an option to auto-increment it.
0
 
ca_hecklerAuthor Commented:
No ofense meant, but could you read the *question* again?
What I want is to programmatically get the version for any generic file.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I think with the below function (GetFileInfo) you get what you need. The declare function are the API requirements for the function (i hope i didn't miss any as i copied out of my application code)

Private Declare Function GetFileVersionInfo Lib "Version.dll" _
   Alias "GetFileVersionInfoA" _
   (ByVal lptstrFilename As String, _
   ByVal dwhandle As Long, _
   ByVal dwlen As Long, _
   lpData As Any) As Long

Private Declare Function GetFileVersionInfoSize Lib "Version.dll" _
   Alias "GetFileVersionInfoSizeA" _
   (ByVal lptstrFilename As String, _
   lpdwHandle As Long) As Long

Private Declare Function VerQueryValue Lib "Version.dll" _
   Alias "VerQueryValueA" _
   (pBlock As Any, _
   ByVal lpSubBlock As String, _
   lplpBuffer As Any, _
   puLen As Long) As Long


Private Declare Sub MoveMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
   (dest As Any, _
   ByVal Source As Long, _
   ByVal Length As Long)

Private Declare Function lstrcpy Lib "kernel32" _
   Alias "lstrcpyA" _
   (ByVal lpString1 As String, _
   ByVal lpString2 As Long) As Long


Public Sub GetFileInfo(strFile As String, ByRef filDate As Date, _
  ByRef filVersion As String, ByRef filLanguage As Long)
 
  Dim lBufferLen As Long
  Dim lDummy As Long
  Dim sBuffer()  As Byte
  Dim bytebuffer(255) As Byte
  Dim lVerPointer As Long
  Dim Lang_Charset_String As String
  Dim HexNumber As Long

  Dim strTemp As String
  Dim strBuffer As String
 
  Dim rc As Long
 
  lBufferLen = GetFileVersionInfoSize(strFile, lDummy)
  If lBufferLen > 0 Then
 
    ReDim sBuffer(lBufferLen)
    rc = GetFileVersionInfo(strFile, 0&, _
                             lBufferLen, _
                             sBuffer(0))
    If rc <> 0 Then
      rc = VerQueryValue(sBuffer(0), "\VarFileInfo\Translation", _
                        lVerPointer, _
                        lBufferLen)
                       
      MoveMemory bytebuffer(0), lVerPointer, lBufferLen
      HexNumber = bytebuffer(2) + bytebuffer(3) * &H100 + _
                  bytebuffer(0) * &H10000 + bytebuffer(1) * &H1000000
      Lang_Charset_String = Hex(HexNumber)
      Do While Len(Lang_Charset_String) < 8
         Lang_Charset_String = "0" & Lang_Charset_String
      Loop
           
           
      strBuffer = String(255, 0)
      strTemp = "\StringFileInfo\" & Lang_Charset_String _
        & "\FileVersion"
      rc = VerQueryValue(sBuffer(0), strTemp, _
                          lVerPointer, lBufferLen)

      lstrcpy strBuffer, lVerPointer
      strBuffer = Mid$(strBuffer, 1, InStr(strBuffer, Chr(0)) - 1)
           
      filLanguage = HexNumber
      filDate = FileDateTime(strFile)
      filVersion = strBuffer
    Else
      filDate = Empty
      filVersion = ""
      filLanguage = 0
    End If
  Else
    filDate = Empty
    filVersion = ""
    filLanguage = 0
  End If
End Sub



CHeers
0
 
ca_hecklerAuthor Commented:
Awesome! You're da man, thanks!
0
 
Derek0903Commented:
Thanks - this code is great - saved me a bunch of time..

Cheers..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now