Find The Program That Associated With Certain File

This code will tell you what is the program that opens certain file type, for example: If WinAmp is openning when you double click on MP3 file, then this code will return "C:\Program Files\WinAmp\Winamp.exe" .


Module Code

Declare Function FindExecutable Lib _
    "shell32.dll" Alias "FindExecutableA" _
    (ByVal lpFile As String, ByVal lpDirectory _
    As String, ByVal lpResult As String) As Long

Declare Function GetTempFileName Lib _
    "kernel32" Alias "GetTempFileNameA" (ByVal _
    lpszPath As String, ByVal lpPrefixString _
    As String, ByVal wUnique As Long, ByVal _
    lpTempFileName As String) As Long

Declare Function GetTempPath Lib _
    "kernel32" Alias "GetTempPathA" (ByVal _
    nBufferLength As Long, ByVal lpBuffer As _
    String) As Long

Form Code

Public Function GetAssociatedProgram(ByVal _
    Extension As String) As String

    Dim Path As String
    Dim FileName As String
    Dim nRet As Long
    Const MAX_PATH As Long = 260
   
    'Create a temporary file
    Path = String$(MAX_PATH, 0)
   
    If GetTempPath(MAX_PATH, Path) Then
        FileName = String$(MAX_PATH, 0)
   
        If GetTempFileName(Path, "~", 0, FileName) Then
            FileName = Left$(FileName, _
                InStr(FileName, vbNullChar) - 1)
       
            'Rename it to use supplied extension
            Name FileName As Left$(FileName, _
                InStr(FileName, ".")) & Extension
                FileName = Left$(FileName, _
                InStr(FileName, ".")) & Extension
       
            'Get name of associated EXE
            Path = String$(MAX_PATH, 0)
       
            Call FindExecutable(FileName, _
                vbNullString, Path)
            GetAssociatedProgram = Left$( _
                Path, InStr(Path, vbNullChar) - 1)
       
            'Delete the temporary file
            Kill FileName
       
        End If
   
    End If

End Function

Private Sub Form_Load()
' Replace the "MP3" below with the file extention you want to check
' Its default program
    MsgBox GetAssociatedProgram("MP3")
End Sub

Go Back