Friday, June 22, 2018

Finding the default application for a file extension in WIndows

Copyright © 2018, Steven E. Houchin. All rights reserved.

A client of mine has a Windows application that lists various data files, and when a data file is clicked, opens the associated application for the file's extension, using an established API to do that.  It has worked fine for a long time, but gives unexpected results on Windows 10 for html files. The default program for html on the system is configured for MS Edge, but a click on the html file in the application always brought up Internet Explorer.

Sure enough, following the traditional file association scheme in the Registry showed IE, not Edge.  The traditional scheme is:

HKEY_CLASSES_ROOT\.html
    (Default) = program_identifier
HKEY_CLASSES_ROOT\program_identifier\shell\open\command
    (Default) = full path to default program

By doing more research, I discovered that M*Soft changed the file association scheme starting, I think, with Vista.  The old scheme may still work for some file types, but finding the right browser requires using the new scheme.

The new post-Vista scheme I found requires looking into file associations for Window Explorer and following from there.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\
  FileExts\.html\UserChoice
    ProgId = program_identifier
HKEY_CLASSES_ROOT\program_identifier\shell\open\command
    (Default) = full path to default program

In the new scheme, for MS Edge, the ProgId value could be something strange like "AppXq0fevzme2pys62n3e0fbqa7peapykr8v", whereas the Firefox browser may be "FirefoxURL". The obvious advantage of this new scheme is that using HKEY_CURRENT_USER allows for user-by-user default program setting, where, I think, the traditional scheme sets it for all users.

So, by writing my own Registry traverser for the new scheme, I finally got MS Edge to open as the default browser. If the new scheme fails to find the default program, my code falls back to the traditional scheme shown before.