So a recent item that I've been tasked with is programmatically finding the application that is required to open/load a file of a given file extension. One would think that this would be a straightforward task and quite easy to accomplish with .Net.
My first thought was to check the System.Environment namespace for any mappings of file extensions. Nada. My next instinct was to check System.Runtime.InteropServices to see if there was a chance that some object (maybe Marshal) had access to such a table. No go. I also stopped by Microsoft.Win32...nothing as well.
I did come across an interesting method in System.Drawing.Icon, ExtractAssociatedIcon() which would be useful if that was what I was after. Unfortunately, I didn't find anything that would resemble this functionlity in System.IO.
So it would seem that for the time being, the only way that I can get at this data is to read registry keys.
What I've found is the following algorithm:
- Open HKEY_CLASSES_ROOT\<extension>\(Default). This gives us the class name that handles the file extension.
- Open HKEY_CLASSES_ROOT\<class-name>\CLSID\(Default). This gives us the GUID class ID of the application that handles the file extension.
- Open HKEY_CLASSES_ROOT\CLSID\<class-id>\InProcServer32|LocalServer32\(Default). This will yield the path to the executable which handles the file extension.
In some cases, it is enough to open the first key (the extension key) and you will find a subkey called OpenWithList with the first subkey being the name of the executable of the application (but this isn't always available).
I'm thinking there has to be a better way to do this programmatically, but I haven't come across it yet.