6

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?

Simon Martin
  • 4,203
  • 7
  • 56
  • 93
ChrisFletcher
  • 1,010
  • 2
  • 14
  • 31

5 Answers5

5

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
  • 61
  • 1
  • 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 Jan 14 '10 at 16:25
3

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
  • 72,686
  • 18
  • 132
  • 173
2
Process.Start("explorer.exe",file) 

might be worth a try as well.

Solves the issue I had of opening URLs across XP,Vista, and 7

Jamona Mican
  • 1,574
  • 1
  • 23
  • 54
1

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
  • 733,204
  • 149
  • 1,241
  • 1,454
0

If you build it for ASP.NET use the code below to avoid " access is denied" error

Response.Redirect(url);
Serge V.
  • 3,377
  • 3
  • 20
  • 28