I'm trying to figure out the executable used to open files of a given extension, so I can display that executable's icon if the extension doesn't have one.
I'm aware of the open verb in the HKEY_CLASSES_ROOT registry key for the file type, but I've found that it's value is not always correct.
For example, I'm currently running Windows in Parallels on OS X. The default association of PDF files was Safari. I changed the default association, through explorer, to Adobe Reader. The open verb in the registry is still Safari, but when I double click a PDF file, it opens with Adobe Reader. Both the 32-bit and 64-bit registry have the same values.
Is there a better way to retrieve the association of a file type, either with .NET or winapi?
Your best bet is probably to use the Assoc*
group of functions, such as AssocQueryKey()
and AssocQueryString()
via PInvoke. I don't know, however, whether one of the many classes that come with the .NET framework in its many incarnations wraps this for you already. But the Shell API gives you an option to retrieve this information.
Also note the remark section which states that the above functions are wrappers for the IQueryAssociations
interface, which makes it even more likely you have another more straight route to what you want from within .NET.
Old-style function was this one: FindExecutable()
. Don't use it, though. It uses the same flawed error code magic as ShellExecute()
.
Also see the answers for Windows: List and Launch applications associated with an extension