简体   繁体   English

替代 Process.Start()

[英]Alternative to Process.Start()

I'm just finishing off coding aa document storage solution and I've run across the following issue.我刚刚完成对文档存储解决方案的编码,我遇到了以下问题。 Within the UI the user can press a button to open a file:在 UI 中,用户可以按下一个按钮来打开一个文件:

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.我的问题是,如果用户没有与文件类型相关联的应用程序,则会引发 componentmodel 异常并显示一条消息。

What I'd rather do is have the "Open with" dialog pop-up in that situation, is there a method call I'm missing?我宁愿做的是在这种情况下弹出“打开方式”对话框,是否有我缺少的方法调用?

You can check the registry to see if you have an application associated with that file type before calling Process.Start.您可以在调用 Process.Start 之前检查注册表以查看是否有与该文件类型关联的应用程序。 Alternatively, you can catch the componentmodel exception and open the open with dialog from there.或者,您可以捕获 componentmodel 异常并从那里打开 open with 对话框。

See this article for using the Open With dialog有关使用“打开方式”对话框的信息,请参阅本文

http://www.codeproject.com/KB/shell/openwith.aspx 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.我会将 Process.Start 调用放在 try 语句中,然后在 catch 中显示“打开方式”。

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解决了我在 XP、Vista 和 7 上打开 URL 的问题

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.只需尝试运行程序,然后在出现异常的情况下,由于文件没有关联,打开一个对话框,允许他们选择一个文件来运行程序。

如果您为 ASP.NET 构建它,请使用下面的代码来避免“访问被拒绝”错误

Response.Redirect(url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM