How to bring up the "Windows cannot open this file" dialog?
Asked Answered
R

2

5

My users can attach documents to various entities in the application. Of course, if user A attaches a .TIFF file, user B may not have a viewer for that type of file.

So I'd like to be able to bring up this dialog:

alt text http://www.angryhacker.com/toys/cannotopen.png

My application is C# with VS2005.
Currently I do Process.Start and pass in the file name. If no association is found, it throws an exception.

Rich answered 19/5, 2009 at 17:16 Comment(0)
I
7

This should do it:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "rundll32.exe";
p.StartInfo.Arguments = "shell32.dll,OpenAs_RunDLL " + yourFileFullnameHere;

p.Start();
Impassable answered 19/5, 2009 at 17:28 Comment(2)
The problem with this approach is that it brings up the dialog box every time. I guess I should just execute this code if I get a Win32Exception stating that there is no association.Rich
Note that OpenAs_RunDLL is undocumented and does not always work.Rhinology
R
14
Process pr = new Process();
pr.StartInfo.FileName = fileTempPath;
pr.StartInfo.ErrorDialog = true; // important
pr.Start();
Rayleigh answered 30/10, 2009 at 9:57 Comment(1)
This one's better, platform independent.Dunaville
I
7

This should do it:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "rundll32.exe";
p.StartInfo.Arguments = "shell32.dll,OpenAs_RunDLL " + yourFileFullnameHere;

p.Start();
Impassable answered 19/5, 2009 at 17:28 Comment(2)
The problem with this approach is that it brings up the dialog box every time. I guess I should just execute this code if I get a Win32Exception stating that there is no association.Rich
Note that OpenAs_RunDLL is undocumented and does not always work.Rhinology

© 2022 - 2024 — McMap. All rights reserved.