Determining the associated application

Knowing the application associated with a given file extension.

WHERE IS THAT INFORMATION?

The applications associated with the file extensions are stored in the Windows Registry. To get this information first we should retrieve the "class" that a file extensions belongs to. This information can be found at:



HKEY_CLASSES_ROOT\.ext\(default)



where ".ext" is the file extension you want (like ".txt", ".bmp", etc.). Then we get the command line used to open that kind of files. To do that, we retrieve the data under



HKEY_CLASSES_ROOT\class\Shell\Open\Command\(default)



where "class" is the file class an extension belongs to. That string usually has the form



"D:\PATH\APPNAME.EXT" "%1" -OPTIONS



where %1 is a placeholder for the document file to open with the application, so we should find its position within the string and replace it with the filename we want to open.





EXAMPLE

The following function returns the command line of the associated application to open a documente file:



uses Registry, Windows, SysUtils;



  function GetAssociation(const DocFileName: string): string;

  var

    FileClass: string;

    Reg: TRegistry;

  begin

    Result := '';

    Reg := TRegistry.Create(KEY_EXECUTE);

    Reg.RootKey := HKEY_CLASSES_ROOT;

    FileClass := '';

    if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then

    begin

      FileClass := Reg.ReadString('');

      Reg.CloseKey;

    end;

    if FileClass <> '' then begin

      if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then

      begin

        Result := Reg.ReadString('');

        Reg.CloseKey;

      end;

    end;

    Reg.Free;

  end;

 

Share this article!

Follow us!

Find more helpful articles: