views:

518

answers:

3

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?

+4  A: 

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.

Tal Pressman
In other words, if it's there to be found in the registry, it'll be picked up when I pass Process.Start(filename), and I was basically looking to duplicate existing, automatic functionality? Well, at least I learned a thing or two about reading registry keys!
AgentConundrum
Yup, it's really that easy.
Nifle
+2  A: 

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");
Dan Walker
A: 

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).

Fredrik Mörk
I just tried this with "Process.Start(@"C:\myfile.lol");" where lol isn't associated with anything. This didn't bring up the default "ask what to use" dialog, it threw a Win32Exception - "No application is associated with the specified file for this operation."
AgentConundrum
Maybe this means it's worth it to see if the filetype is in the registry. Handle it gracefully if there's nothing there, otherwise just pass the filename to Process.Start() and let it sort out the association.
AgentConundrum
I don't get any exception, as long as the file exists (on Vista). If the file does not exist I get a Win32Exception (The system cannot find the file specified).
Fredrik Mörk
That's odd. Maybe it's because I'm on XP, but Process.Start("notepad.exe", @"C:\myfile.lol"); works fine, but Process.Start(@"C:\myfile.lol") throws the Win32Exception. This isn't a typo with the filename, because I didn't edit that string when I tested it with Notepad.
AgentConundrum
That could very well be an XP vs. Vista issue. Sorry my answer didn't help out then... :o)
Fredrik Mörk