Results 1 to 9 of 9

Thread: [2008] Show Windows "Open With" dialog when no file association found

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    [2008] Show Windows "Open With" dialog when no file association found

    Hi,

    I am using Process.Start(fileName) to let Windows open certain files outside of my application with their 'default application'.

    If "fileName" for example is a ".txt" file, it opens the file in Notepad. If it is a folder, it opens windows explorer with that folder selected.

    This all seems to work well, except when the user enters a file that has no application 'linked' to it... (a file that has no file associations)

    I have wrapped the Process.Start() in a Try/Catch block so that any errors are caught and displayed.
    When I now open a file without any file associations (windows does not know with which application to open it) it tells me that it cannot find any application to open it with. (The exact error is in dutch so I don't think that's going to be much help here, but I think it's clear what's happening, no?)


    What I would like to happen in this case is that the windows "Open With" dialog comes up, asking the user to select an application to open the file with.
    Now it just displays the error message and continues, making it impossible for the user to open the file unless he browses to the file manually and sets up the file association, something many users probably don't know how to do...


    So is this possible?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [2008] Show Windows "Open With" dialog when no file association found

    Try this:
    Code:
    Private Sub OpenWith(ByVal filePath As String)
            Dim psi As New ProcessStartInfo("rundll32.exe")
            With psi
                .Arguments = String.Format("shell32.dll,OpenAs_RunDLL {0}", filePath)
            End With
            Process.Start(psi)
        End Sub
    To use it, you just call the sub and pass in the full path to the file you want to use "open with".
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Show Windows "Open With" dialog when no file association found

    Thanks, it does work, but it doesn't do exactly what I want it to do unfortunately...

    Now, it always seems to show the Open With dialog, even if there IS a file association. If there already is a file association, I want it to open with that default application and not ask the user.

    I only want it to ask the user when windows cannot find any file association.


    I suppose I could always just try to "Process.Start" the file normally, and try the "Open With" solution when it encounters an error, but that seems like a dirty workaround, there surely is a better way?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [2008] Show Windows "Open With" dialog when no file association found

    You just put Process.Start(filePath) in a try/catch block, and in the catch block, you use open with.
    Code:
    Private Sub OpenThisFile(ByVal filePath As String)
            If System.IO.File.Exists(filePath) Then
                Try
                    Process.Start(filePath)
                Catch ex As Exception
                    Dim psi As New ProcessStartInfo("rundll32.exe")
                    psi.Arguments = String.Format("shell32.dll,OpenAs_RunDLL {0}", filePath)
                    Process.Start(psi)
                End Try
            Else
                MessageBox.Show("File doesn't exist!")
            End If
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] Show Windows "Open With" dialog when no file association found

    Or you can look in the registry to see if the file type has an association.
    Code:
            'HKEY_CLASSES_ROOT 
            Dim HKCR As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot
    
            'extension to test for
            Dim FileType As Microsoft.Win32.RegistryKey = HKCR.OpenSubKey(".hght")
    
            If FileType IsNot Nothing Then
                MessageBox.Show("There is a 'default application'.")
            Else
                MessageBox.Show("No default application.")
            End If
    VB 2005, Win Xp Pro sp2

  6. #6
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,965

    Re: [2008] Show Windows "Open With" dialog when no file association found

    stanav ... in your solution is there any way to skip the first screen (see below) & go straight to the second screen where you actually select the application you want to use?

    .
    Attached Images Attached Images  

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [2008] Show Windows "Open With" dialog when no file association found

    I don't think there's a way to bypass that 1st dialog. That's how Windows works. If you try to open a file with unregistered file type manually by double-click on it, you'll see that 1st dialog. If you try right-click on the file, you won't see the option "Open With..." as with a registered file type.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] Show Windows "Open With" dialog when no file association found

    I think I've seen some registry hack to turn off that first prompt but nobody reall likes programs changing global settings (esp. when unannounced). Also, to combine the given suggestions so far, there is a property in the ProcessStartInfo class that does what NickThissen wants (mentioned last here).
    Code:
            Dim openWithPrInfo As New ProcessStartInfo
    
            openWithPrInfo.FileName = "c:\asdf.jjj"
            openWithPrInfo.ErrorDialog = True
    
            Process.Start(openWithPrInfo)
    VB 2005, Win Xp Pro sp2

  9. #9

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