Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 814
baron Avatar asked Apr 14 '10 00:04

baron


3 Answers

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

like image 115
AZ. Avatar answered Oct 27 '22 06:10

AZ.


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.

like image 29
slugster Avatar answered Oct 27 '22 06:10

slugster


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.

like image 29
dthorpe Avatar answered Oct 27 '22 05:10

dthorpe