Determining the associated application (Views: 100)


How can I get the application associated with a document?


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:


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


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


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.


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

function GetAssociation(const DocFileName: string): string;
  FileClass: string;
  Reg: TRegistry;
  Result := '';
  Reg := TRegistry.Create(KEY_EXECUTE);
  FileClass := '';
  if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then
    FileClass := Reg.ReadString('');
  if FileClass <> '' then
    if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then
      Result := Reg.ReadString('');

Copyright (c) 2001 Ernesto De Spirito
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

<< Back to main page