Opening a file using its associated program

G

Gos

Hi,

I have a program that needs to open a list of attachments
displayed on a ListView. I have used Process.Start method to open the
files using their associated program. This works correctly if the file
extention is associated with a process(program/application) already,
but throws an exception when there is no associated process. I would
like to display an "Open With.." dialog box where the user can choose
a program to open the file.

Can anyone please help me with this?

Thank you very much
Eric
 
H

Herfried K. Wagner [MVP]

(e-mail address removed) (Gos) scripsit:
I have a program that needs to open a list of attachments
displayed on a ListView. I have used Process.Start method to open the
files using their associated program. This works correctly if the file
extention is associated with a process(program/application) already,
but throws an exception when there is no associated process. I would
like to display an "Open With.." dialog box where the user can choose
a program to open the file.

You can catch the exception and call the function below:

PInvoke, seems to be undocumented:

\\\
Private Declare Function OpenWithDialog Lib "shell32.dll" _
Alias "OpenAs_RunDLL" ( _
ByVal hWndOwner As IntPtr, _
ByVal dwUnknown1 As Int32, _
ByVal strFileName As String, _
ByVal dwUnknown2 As Int32 _
) As Int32

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
OpenWithDialog(Me.Handle, 0, "C:\AUTOEXEC.BAT", 0)
End Sub
///
 
T

Tom Shelton

Hi,

I have a program that needs to open a list of attachments
displayed on a ListView. I have used Process.Start method to open the
files using their associated program. This works correctly if the file
extention is associated with a process(program/application) already,
but throws an exception when there is no associated process. I would
like to display an "Open With.." dialog box where the user can choose
a program to open the file.

Can anyone please help me with this?

Thank you very much
Eric

Eric,

Set the process class's StartInfo.ErrorDialog property to true... This
will cause the Open With... dialog to appear if the process can't be
started.

Tom Shelton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top