Is it worth looking for the default app in the registry when opening a file from a C # app?

I am creating an application (a side project that will most likely attract the help of the stackoverflow community more than once) that will need to open many types of files (i.e. open Word documents in Word, not natively in my application).

I've been playing around with some code to find the default application for the file type in the registry and pass it to Process.Start (). There seem to be two questions with this approach:

1) Application name is quoted in some cases and not others.
2) Process.Start () requires the application path and its arguments to be passed separately (ie Process.Start ("notepad.exe", @ "C: \ myfile.txt"), not Process.Start (@ "notepad .exe C: \ myfile.txt");).

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

The alternative is to just pass in 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 general solution for parsing arguments?

Thanks for any help!

Update:
I am guessing the short answer is "No"
It seems that I have indeed taken the overflow route and passing only the filename will work whenever there is an associated value in the registry. That is, whatever I find in the registry on my own, Process.Start () already knows how to do it.

I found that when I try to use this with a "new" file type, I get a Win32Exception that says "No application is associated with the specified file for this operation." Fredrik MΓΆrk mentions in a comment that this doesn't happen for him in Vista. What's the correct way to handle this?

0


a source to share


3 answers


If an extension is registered to open in a specific application, it does not need to be in the PATH to run.



+5


a source


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");

      

+2


a source


You usually don't need to look for a program for the registered types, and the program usually shouldn't be in your PATH environment variable. Usually the command in the registry contains the full path. This is how the command looks for .kml files (Google Earth) (on my computer):

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

      

With that in mind, you can simply use Process.Start

along with the document file names. If the file type is not registered, you will invoke the default Windows behavior to do so (asking which program to use, etc.).

0


a source







All Articles