Solved

File Properties -> Version Number

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

735 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