Process.Start() 的替代方法
我刚刚完成文档存储解决方案的编码,并且遇到了以下问题。在 UI 中,用户可以按按钮打开文件:
try
{
Process.Start(file);
}
catch (Exception ex)
{
//Error handling code
}
我的问题是,如果用户没有与该文件类型关联的应用程序,则会抛出组件模型异常,并显示一条消息。
我宁愿做的是在这种情况下弹出“打开方式”对话框,是否缺少我缺少的方法调用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在调用 Process.Start 之前,您可以检查注册表以查看是否有与该文件类型关联的应用程序。或者,您可以捕获组件模型异常并从那里打开打开对话框。
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.
请参阅本文了解如何使用“打开方式”对话框
http://www.codeproject.com/KB /shell/openwith.aspx
我会将 Process.Start 调用放在 try 语句中,然后在 catch 中显示“Open With”。
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.
也许也值得一试。
解决了我在 XP、Vista 和 7 上打开 URL 的问题
might be worth a try as well.
Solves the issue I had of opening URLs across XP,Vista, and 7
不,没有。我认为你目前的方法是最好的。只需尝试运行该程序,然后在出现异常情况(由于文件没有关联)时,打开一个对话框,允许他们选择一个文件来运行该程序。
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 构建它,请使用下面的代码以避免“访问被拒绝”错误
If you build it for ASP.NET use the code below to avoid " access is denied" error