Bring up a file's Properties box
By: Massimo Branca Date: 20/03/2001

In Windows Explorer, when you right-click a file and select "Properties", a window comes up with various file information: name, type, path, date created, edited, etc. This tip shows you how to do the same thing with Visual Basic code.

First, cut and paste the following API declaration code into the Declarations section of a form.

Private Const SW_SHOW = 5
    cbSize As Long
    fMask As Long
    hwnd As Long
    lpVerb As String
    lpFile As String
    lpParameters As String
    lpDirectory As String
    nShow As Long
    hInstApp As Long
    'Optional fields
    lpIDList As Long
    lpClass As String
    hkeyClass As Long
    dwHotKey As Long
    hIcon As Long
    hProcess As Long
End Type
Private Declare Function ShellExecuteEx _

Lib "shell32.dll" (ByRef s As SHELLEXECUTEINFO) As Long

Second, copy the following Sub into the same form.

Public Sub DisplayFileProperties(ByVal sFullFileAndPathName As String)

    With shInfo
        .cbSize = LenB(shInfo)
        .lpFile = sFullFileAndPathName
        .nShow = SW_SHOW
        .lpVerb = "properties"
    End With

    ShellExecuteEx shInfo
End Sub

The use is very simple: the following code is an example.

Dim sFileName As String
sFileName = "filename"
If Len(Dir(sFileName)) = 0 Then
    MsgBox "File: " & sFileName & " cannot be found"
    Exit Sub
End If
DisplayFileProperties sFileName

Obviously "filename" is the name of the file you want to show properties of (i.e. "C:\Windows\Media\Ding.Wav").

Webmaster - This tip is based on a sample project by Massimo Branca..

Click Here!

Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: