Displaying the Open with… Dialog

When using Process.Start to open a file with its associated application, an exception will be thrown if there is no program associated with the file type. One way to show the system’s Open with… dialog box is to catch this exception and use an undocumented function via P/Invoke:

Imports System.Runtime.InteropServices


Private Declare Auto Function OpenWithDialog _
    Lib "shell32.dll" _
    Alias "OpenAs_RunDLL" _
( _
    ByVal hwnd As IntPtr, _
    ByVal hinst As IntPtr, _
    ByVal lpFile As String, _
    ByVal nShowCmd As Int32 _
) As Int32


OpenWithDialog( _
    Me.Handle, _
    Marshal.GetHINSTANCE(Me.GetType().Module), _
    "C:\AUTOEXEC.BAT", _
    CInt(AppWinStyle.NormalFocus) _

Alternatively (that is the preferred way because it is fully managed) this solution can be used:

Imports System.Diagnostics


Dim psi As New ProcessStartInfo()
psi.ErrorDialog = True
psi.FileName = "C:\AUTOEXEC.BAT"
psi.UseShellExecute = True