Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: Intermediate
May 19, 2003

GetExecutableFile - Retrieving the executable associated to a file

<System.Runtime.InteropServices.DllImport("shell32.dll")> Shared Function _
    FindExecutable(ByVal fileName As String, ByVal fileDir As String, _
    ByVal buffer As System.Text.StringBuilder) As Integer
End Function


' Returns the name and path of the EXEcutable file that is associated to the 
' specified file.
' Returns an empty string if there is no such associated file,
' and raises an error if the file or the path hasn't been found.
' 
' Example: Debug.WriteLine(GetExecutableFile("D:\temp\Db.mdb"))

Public Function GetExecutableFile(ByVal filePath As String) As String
    Const ERROR_FILE_NO_ASSOCIATION = 31&
    Const ERROR_FILE_NOT_FOUND = 2&
    Const ERROR_PATH_NOT_FOUND = 3&
    Const ERROR_FILE_SUCCESS = 32&
    Dim buffer As New System.Text.StringBuilder(260)

    ' call the FindExecutable API function and process the return value
    Select Case FindExecutable(System.IO.Path.GetFileName(filePath), _
        System.IO.Path.GetDirectoryName(filePath), buffer)
        Case ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND
            Throw New System.IO.FileNotFoundException()
        Case ERROR_FILE_NO_ASSOCIATION
            Return ""
        Case Is >= ERROR_FILE_SUCCESS
            ' extract the ANSI string that contains the name of the associated 
            ' executable file
            Return buffer.ToString()
    End Select
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date