Results 1 to 7 of 7

Thread: retrieve file associations

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Location
    Australia
    Posts
    635

    retrieve file associations

    How would I retrieve and sdisplay the Windows file associations into a listbox?
    A.A. Fussy
    Babya Software Group

  2. #2
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    When you say file associations, do you mean get a list of applications on the machine that are designed to open the specific file in question or something else?

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    don't think this can be done in "native" VB, but via API calls you can do pretty much anything, so I'd suggest you dig into the API docs

  4. #4
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Pointer

    Here's a link that illustrates how to "create" file associations,
    maybe it will help?

    http://www.mentalis.org/tips/tip123.shtml
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    FindExecutable() gets the assoicated .EXE file, if there is one.
    This stuff is also in the registry

    This lists the associated file, for one file:

    Code:
    Private Declare Function FindExecutable Lib "shell32" _
       Alias "FindExecutableA" _
      (ByVal lpFile As String, _
       ByVal lpDirectory As String, _
       ByVal sResult As String) As Long
    
    Private Const MAX_PATH As Long = 260
    Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
    Private Const ERROR_FILE_NOT_FOUND As Long = 2
    Private Const ERROR_PATH_NOT_FOUND As Long = 3
    Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
    Private Const ERROR_BAD_FORMAT As Long = 11
    
    Private Sub Command1_Click()
    
       Dim success As Long
       Dim pos As Long
       Dim sResult As String
       Dim msg As String
       
       sResult = Space$(MAX_PATH)
    
      'lpFile: name of the file of interest
      'lpDirectory: location of lpFile
      'sResult: path and name of executable associated with lpFile
       success = FindExecutable("winhlp32.hlp", "c:\winnt\system32\", sResult)
          
       Select Case success
          Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
          Case ERROR_FILE_NOT_FOUND: msg = "file not found"
          Case ERROR_PATH_NOT_FOUND: msg = "path not found"
          Case ERROR_BAD_FORMAT:     msg = "bad format"
          
          Case Is >= ERROR_FILE_SUCCESS:
             
             pos = InStr(sResult, Chr$(0))
             
             If pos Then
                msg = Left$(sResult, pos - 1)
             End If
             
       End Select
       
       MsgBox msg
       
    End Sub
    This gives you all of the assoications on the system in a ListBox:

    Code:
    Private Const MAX_PATH As Long = 260
    Private Const HKEY_CLASSES_ROOT As Long = &H80000000
    Private Const ERROR_SUCCESS As Long = 0
    Private Const vbDot As Long = 46 ' Asc(".") = 46
    Private Const SHGFI_USEFILEATTRIBUTES As Long = &H10
    Private Const SHGFI_TYPENAME As Long = &H400
    Private Const LB_SETTABSTOPS As Long = &H192
    
    Private Type FILETIME
       dwLowDateTime As Long
       dwHighDateTime As Long
    End Type
    
    Private Type SHFILEINFO 
       hIcon As Long
       iIcon As Long
       dwAttributes As Long
       szDisplayName As String * MAX_PATH
       szTypeName As String * 80
    End Type
    
    Private Declare Function RegEnumKeyEx Lib "advapi32" _
       Alias "RegEnumKeyExA" _
       (ByVal hKey As Long, _
       ByVal dwIndex As Long, _
       ByVal lpName As String, _
       lpcbName As Long, _
       ByVal lpReserved As Long, _
       ByVal lpClass As String, _
       lpcbClass As Long, _
       lpftLastWriteTime As FILETIME) As Long
    
    Private Declare Function SHGetFileInfo Lib "shell32" _
       Alias "SHGetFileInfoA" _
      (ByVal pszPath As String, _
       ByVal dwFileAttributes As Long, _
       psfi As SHFILEINFO, _
       ByVal cbFileInfo As Long, _
       ByVal uFlags As Long) As Long
    
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
      (ByVal hwnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       lParam As Any) As Long
    
    
    Private Sub Form_Load()
    
      'Setup tabstops in the listbox by first
      'clearing existing tabs, then setting the
      'new tabstop value.
      
       ReDim TabArray(0) As Long
       
      'only one tabstop
       TabArray(0) = 75
      
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
       Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 1&, TabArray(0))
       List1.Refresh
       
       Command1.Caption = "Get Associations"
    
    End Sub
    
    
    Private Sub Command1_Click()
    
       List1.Clear
       Me.MousePointer = 11
       
      'fill the listbox box with the
      'file types and their extensions
       Call GetAssociatedFileListing
       
       Me.MousePointer = 0
       
    End Sub
    
    
    Private Sub GetAssociatedFileListing()
    
       Dim dwIndex As Long
       Dim sTypeName As String
       Dim sSubkey As String * MAX_PATH
       Dim sClass As String * MAX_PATH
       Dim ft As FILETIME
       
       Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, _
                             dwIndex, _
                             sSubkey, _
                             MAX_PATH, _
                             0, sClass, _
                             MAX_PATH, ft) = ERROR_SUCCESS
          
          If Asc(sSubkey) = vbDot Then
             
            'Pass the returned string to get the file type
             sTypeName = GetFileType(sSubkey)
                
             If Len(sTypeName) > 0 Then
                List1.AddItem TrimNull(sSubkey) & vbTab & sTypeName
             End If
                
          End If
    
          dwIndex = dwIndex + 1
       
       Loop
    
    End Sub
    
    
    Private Function GetFileType(sFile As String) As String
    
      'If successful returns the specified file's
      'typename, returns an empty string otherwise.
      'sFile does not have to exist and can be
      'just a file extension.
       Dim sfi As SHFILEINFO
    
       If SHGetFileInfo(sFile, 0&, _
                        sfi, Len(sfi), _
                        SHGFI_TYPENAME Or SHGFI_USEFILEATTRIBUTES) Then
          GetFileType = TrimNull(sfi.szTypeName)
       End If
    
    End Function
    
    
    Public Function TrimNull(startstr As String) As String
    
      'returns the string up to the first
      'null, if present, or the passed string
       Dim pos As Integer
       
       pos = InStr(startstr, Chr$(0))
       
       If pos Then
          TrimNull = Left$(startstr, pos - 1)
          Exit Function
       End If
      
       TrimNull = startstr
      
    End Function

  6. #6
    Member
    Join Date
    Sep 2003
    Posts
    56
    how do i use the FindExecutable() function?


  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width