没有应用程序与指定的文件异常关联

发布于 2024-08-28 23:33:04 字数 623 浏览 7 评论 0原文

UnhandledException: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)

大家好,

当我尝试使用 Process.Start 打开 .csv 文件时,我在测试的一台机器上遇到以下异常。我认为发生这种情况是因为此框中没有为 .csv 文件设置文件关联。

那么如何避免这种情况呢?

强制 Process.Start 在记事本中打开? - 理想情况下,它应该在 Excel 中打开,但是如果该计算机上不存在 excel,你该怎么办?

谢谢

UnhandledException: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)

Hi everyone,

I am getting the following exception on one machine I am testing on when trying to use Process.Start to open a .csv file. I think this is happening because no file association has been set for .csv files on this box.

So how would you avoid this situation?

Force the Process.Start to open in Notepad? - Ideally it should be opened in excel, but what do you do if excel then doesn't exist on that computer?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痕至 2024-09-04 23:33:04

如果您设置 ProcessStartInfo.ErrorDialog = true,则系统将通过标准 Windows 对话框提示用户:请参阅此处

If you set the ProcessStartInfo.ErrorDialog = true, then the user will be prompt with a standard windows dialog: see here

寄人书 2024-09-04 23:33:04

如果您的应用程序依赖于 Excel 的安装才能正常有效地工作,那么请向用户发出 bug。捕获异常,然后弹出通知告诉他们问题,然后在该通知中为他们提供在记事本等替代编辑器中打开它的选项。

这一切都归结为良好的用户体验——告诉用户,但要通过提供继续的选项来授权他们,而不是仅仅妨碍他们并在发生类似的小问题时停止。

编辑:准确地执行您正在做的操作 - 不要假设他们有 Excel,他们可能有其他查看器/编辑器,例如 OpenOffice。无论注册到 csv 中什么,让它做它该做的事。不要尝试自己去检查文件关联,您的应用程序可能没有(可能不会)有足够的权限在注册表中进行搜索。

您还需要检查其他明显的异常原因,例如用户无权打开目标文件,这可能是由于对文件夹或文件本身的限制造成的。也许文件被锁定是因为它仍在另一个进程中打开。 Process.Start 失败的原因有很多。

捕获异常,如果原因是没有与该文件关联的应用程序,则为他们提供该选项。如果用户选择使用记事本,请尝试在记事本中打开该文件,但仍要注意异常情况。记事本是一个不错的选择,它不会锁定文件,但它仍然受文件夹/文件 ACL 的约束。

If your application depends on Excel being installed to work properly and effectively, then bug the user about it. Catch the exception, and pop up a notification to tell them about the problem, but then in that notification give them an option to open it in an alternative editor such as notepad.

This all boils down to good UX - tell the user, but do it in such a way that you are empowering them by offering options to continue, rather than just getting in their way and stopping when a little problem like that occurs.

Edit: Do exactly what you are doing - don't assume that they have Excel, they may have some other viewer/editor like OpenOffice. Whatever is registered to csv, let it do it's thing. Don't try to go and check the file association yourself, your app may not (probably won't) have sufficient privileges to go fossicking around in the registry.

You also need to check for other obvious reasons for exceptions, like the user doesn't have rights to open the target file, this could be due to restrictions placed on the folder or the file itself. Maybe the file is locked because it is still open in another process. There are a bunch of reasons why your Process.Start could fail.

Catch the exception, and if the cause is no application associated with the file then offer them the option. If the user chooses to use Notepad, try and open the file in Notepad, but still watch out for exceptions. Notepad is a good option, it doesn't hold a lock on the file, but it is still subject to folder/file ACLs.

春庭雪 2024-09-04 23:33:04

在执行 process.start 之前,请先读取注册表以查看是否存在与该文件扩展名关联的程序。查看 HKEY_CLASSES_ROOT\.csv 查看谁注册来处理该文件扩展名(如果有)。

Read the registry to see if there is a program associated with the file extension before you do the process.start. Look in HKEY_CLASSES_ROOT\.csv to see who is registered to handle that file extension, if any.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文