views:

681

answers:

2
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

+1  A: 

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.

dthorpe
and what if nothing is associated? force open in notepad (id say its fairly safe to assume most xp boxes would have this) and if there is association - open with the associated app? hopefully your able to get the process name out of the registry easily enough?...
baron
If nothing is associated to handle the file type, then what you do with it is up to you - Notepad might be an ok choice. If your peek at the registry indicates that something is registered to handle the file type, I would let Process.Start handle the details of actually deciphering the registry Open verbs and whatnot.
dthorpe
+3  A: 

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.

slugster
it doesn't depend on it - but for the user its much easier to read a CSV file like that. How can I check if excel is installed?
baron
See my edit....
slugster
Your response makes perfect sense. Cheers
baron