views:

98

answers:

3

I want to present the user with a list of known file extensions for him to pick. I know that these are stored in the Registry under HKEY_CLASSES_ROOT usually like this:

.txt -> (default)="txtfile"

where txtfile then contains the information about associated programs etc.

Unfortunately that place in the registry also stores lots of other keys, like the file types (e.g. txtfile) and entries like

CAPICOM.Certificates (whatever that is)

How do I determine which of the entries are file extensions? Or is there a different way to get these extensions like an API function?

(I don't think it matters, but I am using Delphi for the program.)

+1  A: 

IMHO - all those registry subkeys starting with the dot (.) - are for file extensions.

For instance in your case .txt stands for the "txt" extension, whereas txtfile doesn't start with the dot.

valdo
Is that your guess (I guessed that as well) or is it documented somewhere? I found documentation for HKEY_CLASSES_ROOT in MSDN but it did not clarify which entries are extensions and which are not.
dummzeuch
This is my "scientific guess". I tried to add a new subkey under HKEY_CLASSES_ROOT starting with dot. Then added the corresponding stuff, and finally called 'SHChangeNotify'. And - voila! this worked!
valdo
My impression is that "txtfile" is a level of indirection. You can have e.g. "txtfile" and several dot-style entries pointing to it, e.g. ".txt", ".ini", ".csv" which then all point to "txtfile" where the shared settings are stored.
TOndrej
+1  A: 

There is no guarantee that every keys preceded by a dot in HKEY_CLASSES_ROOT is intended for file association, but every file association requires creation of a key preceded by a dot. See MSDN on File Types topic.

AFAIK, the method I describe here conforms with how the Windows Set File Associations feature works to get a list of all known file types. It was based on my former observation when I delved into this subject.

To achieve that, you'll need to do intricate steps as follows:

  1. Enumerating every keys preceded by a dot . , you can use RegQueryInfoKey() and RegEnumKeyEx() for this purpose.

  2. In every keys preceded by a dot, look at the default value data:

    a. If the default value is not empty, this is enough indication that the "preceding dot key" is intended for file association in all Windows NT version, then try to open the key name as mentioned by the value data, just says TheKeyNameMentioned.

    a1) If there is subkeys shell\open\command under TheKeyNameMentioned, then test the existence of the path pointed by the default value of this key; if the path exists, there is a default application associated with the extension; if the path doesn't exists, the default application is unknown. To get the file extension description, look at the default value of TheKeyNameMentioned. To get the program description, first, test whether the following key contain a value-name equal to the EXE file path, that is HKCR\Local Settings\Software\Microsoft\Windows\Shell\MuiCache. If it is there, then look at the value data to get the file description; if it is not there, use GetFileVersionInfo() directly to get the file description.

    a2) If there is no subkeys shell\open\command under TheKeyNameMentioned, then the default application is unknown. To get the file extension description, look at the default value of TheKeyNameMentioned.

    b. On Windows Vista and later, when the point [a] fails, you need additional check. If the default value is empty, test whether the key has a subkey named OpenWithProgIDs.

    • If OpenWithProgIDs subkey exists, use RegEnumValue() to find the first encountered value name that meets the criteria, that is, the name of the value name must point to an existing key (just says TheKeyNameMentioned.) with the same name as the value name. If TheKeyNameMentioned exists, this is enough indication that the "preceding dot key" is intended for file association. Read point a1 and a2 for the next steps.

    • If OpenWithProgIDs subkey doesn't exist, the default application is unknown. To get the file extension description, look at the default value of TheKeyNameMentioned.

Hope that helps. :-)

Vantomex
I have made some additional information to my answer.
Vantomex
Don't rely on looking at just the `shell\open\command` subkey. Any custom verb name can be registered underneath the `shell` key, not just `open`, and not all verbs use the `command` subkey, some use the `ddeexec` or `DropTarget` subkey instead.
Remy Lebeau - TeamB
A: 

I don't know the details, but it seems you could use the IQueryAssociations interface.

TOndrej