Solved

File Properties -> Version Number

Posted on 2002-05-30
5
3,439 Views
Last Modified: 2008-08-27
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
Comment
Question by:ca_heckler
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7044547
from the Project Properties, into the Make tab, you have the version number and an option to auto-increment it.
0
 
LVL 1

Author Comment

by:ca_heckler
ID: 7044567
No ofense meant, but could you read the *question* again?
What I want is to programmatically get the version for any generic file.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 7044605
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
 
LVL 1

Author Comment

by:ca_heckler
ID: 7044636
Awesome! You're da man, thanks!
0
 

Expert Comment

by:Derek0903
ID: 12622415
Thanks - this code is great - saved me a bunch of time..

Cheers..
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question