ChrisFletcher
ChrisFletcher

Reputation: 1042

Alternative to Process.Start()

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?

Upvotes: 7

Views: 6382

Answers (5)

Serge V.
Serge V.

Reputation: 3623

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

Response.Redirect(url);

Upvotes: 0

Jamona Mican
Jamona Mican

Reputation: 1654

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

Upvotes: 2

JaredPar
JaredPar

Reputation: 755141

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.

Upvotes: 1

David
David

Reputation: 73574

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.

Upvotes: 3

jmaresca
jmaresca

Reputation: 61

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.

Upvotes: 5

Related Questions