views:

252

answers:

3

Hi, I'm just finishing off coding a a document storage solution and I've run across the following issue. Within the UI the user can press a button to open a file:

try
            {
                Process.Start(file);
            }
            catch (Exception ex)
            {
              //Error handling code
            }

My issue is that if the user has no application associated with the file type a componentmodel exception is thrown with a message to that effect.

What I'd rather do is have the open with dialog pop-up in that situation, is there a method call I'm missing?

+3  A: 

You can check the registry to see if you have an application associated with that file type before calling Process.Start. Alternatively, you can catch the componentmodel exception and open the open with dialog from there.

jmaresca
Use code from http://stackoverflow.com/questions/4638/how-do-you-create-your-own-moniker-url-protocol-on-windows-systems for examples on how to check you have a URL protocol installed
Brett Veenstra
+1  A: 

See this article for using the Open With dialog

http://www.codeproject.com/KB/shell/openwith.aspx

I would put the Process.Start call in a try statement, and then show the "Open With" in the catch.

David Stratton
+1  A: 

No there is not. I think your current approach is the best. Simply attempt to run the program and then in case of an exception, due to the file having no association, open up a dialog allowing them to select a file to run the program.

JaredPar