简体   繁体   中英

No application is associated with the specified file exception

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

如果设置ProcessStartInfo.ErrorDialog = true,则用户将使用标准窗口对话框进行提示:请参阅此处

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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