Solved

File Properties -> Version Number

Posted on 2002-05-30
5
3,408 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 69

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now