0
votes

I'm building an application (a side project which is likely to enlist the help of the stackoverflow community on more than one occasion) which will need to open a variety of file types (i.e. open Word documents in Word, not natively in my application).

I've been playing with some code for looking up the default application for the file type in the registry and passing this to Process.Start(). There seem to be two issues with this approach:

1) The application name is quoted in some instances, and not in others.
2) Process.Start() requires that the application path and it's arguments are passed separately (i.e. Process.Start("notepad.exe", @"C:\myfile.txt"); rather than Process.Start(@"notepad.exe C:\myfile.txt");).

This means when I retrieve the path from the registry, I have to split it (after determining if I need to split on quotes or spaces) to determine what part is the application path and what parts are arguments, then pass those separately to Process.Start().

The alternative seems to be to just pass the filename, as in Process.Start(@"C:\myfile.txt"), but I think this only works if the application is in the Path environment variable.

Which way is better? In the case of the registry, is there a common solution for how to do the argument parsing?

Thanks for any and all help!

Update:
I guess the short answer is 'No.'
It seems like I was really going the overkill route, and that passing just the filename will work whenever there's an associated value in the registry. I.e. anything I find in the registry myself, Process.Start() already knows how to do.

I did discover that when I try this with a "new" filetype, I get a Win32Exception stating "No application is associated with the specified file for this operation." Fredrik Mörk mentions in a comment that this doesn't occur for him in Vista. What's the proper way to handle this?

3

3 Answers

5
votes

If the extension is registered to be opened with a certain application, it doesn't need to be in the PATH in order to run.

2
votes

The application does not need to be in the PATH if you only specify the filename. The following code worked fine for me:

            System.Diagnostics.Process.Start(@"C:\Users\Dan\Desktop\minors.pdf");
0
votes

You typically do not need to lookup the program for registered types, and the program does not typically need to be in the PATH environment variable. Usually the command in the registry contains the full path. This is how the command for .kml files (Google Earth) looks (in my computer):

C:\Program Files\Google\Google Earth\googleearth.exe "%1"

Given that, you can safely just use Process.Start together with the document file names. Should it be that the file type is not registered you will invoke the default Windows behaviour for this (asking you which program to use, and so on).