Jump to content
PeterPanettone

Get the list of all file extensions associated by double-click with a specific application?

Recommended Posts

Posted (edited)

When I try to get the list of all file extensions from the Registry associated with a specific application (double-clicking on the file opens the application), I encounter a complex, almost impenetrable maze of multiple, countless references and back-references that seems almost impossible to resolve.

 

It would be helpful if there were a "magical" function like GetListOfAssociatedFileExtensions('notepad.exe', 'open');

 

Is there a known solution to this problem?

 

For a test, I've started by enumerating all file-extension subkeys in the Registry:
 

function EnumerateFileExtensions: TStringList;
var
  reg: TRegistry;
  subkeys: TStringList;
  i: Integer;
  key: string;
begin
  Result := TStringList.Create;
  subkeys := TStringList.Create;
  reg := TRegistry.Create(KEY_READ);
  try
    reg.RootKey := HKEY_CLASSES_ROOT;

    // Open the key for reading
    if reg.OpenKeyReadOnly(key) then
    begin
      reg.GetKeyNames(subkeys);
      reg.CloseKey;

      // Log the retrieved subkeys
      CodeSite.Send('Subkeys:', subkeys.Text);

      // Iterate through subkeys and filter those starting with a dot
      for i := 0 to subkeys.Count - 1 do
      begin
        if Subkeys[i][1] = '.' then
        begin
          // Add the subkey to the result list
          Result.Add(subkeys[i]);
        end;
      end;
    end;
  finally
    reg.Free;
    subkeys.Free;
  end;
end;

 

Edited by PeterPanettone

Share this post


Link to post
Posted (edited)
1 hour ago, PeterPanettone said:

Is there a known solution to this problem?

Unfortunately, it is not so simple.  There are many different ways for file types to be associated with an app.  The Registry keys you are currently looking at (the ones starting with a dot) is just the beginning.  Those are static verb registrations, and the most common approach.  But there are other ways, too.  Especially when you start taking into account things like dynamic registrations, Shell COM objects for File Type handlers and Context Menu handlers, etc.  A lot can go on behind the scenes when the OS is asked to open an app associated with a given file.

Edited by Remy Lebeau
  • Like 2

Share this post


Link to post
6 hours ago, Remy Lebeau said:

Unfortunately, it is not so simple.

That's why I wrote "multiple, countless references and back-references."

 

Has no one really ever written a function to enumerate the list of file extensions associated with a specific application?

Share this post


Link to post
8 hours ago, PeterPanettone said:

There is a function AssocQueryString in shlwapi.dll.

AFAIK, that function cannot return a list of file extensions for a given application.  It is typically used the other way around - given a file type, it can return associated data, such as the application exe/command that will open the file.

Share this post


Link to post
10 hours ago, PeterPanettone said:

Has no one really ever written a function to enumerate the list of file extensions associated with a specific application?

Not that I'm aware of, because 99% of apps don't need that kind of information.  What is your use-case for retrieving such a list?

Share this post


Link to post
On 3/16/2024 at 6:24 PM, Remy Lebeau said:

What is your use-case for retrieving such a list?

Managing applications in general by giving the user the information on which file extensions open a specific application. This information lets the user decide which file extensions are allowed or should open a particular application.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×