Author: Ernesto De Spirito
How can I get the application associated with a document?
Answer:
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:
1 2 function GetAssociation(const DocFileName: string): string;
3 var4 FileClass: string;
5 Reg: TRegistry;
6 begin7 Result := '';
8 Reg := TRegistry.Create(KEY_EXECUTE);
9 Reg.RootKey := HKEY_CLASSES_ROOT;
10 FileClass := '';
11 if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then12 begin13 FileClass := Reg.ReadString('');
14 Reg.CloseKey;
15 end;
16 if FileClass <> '' then17 begin18 if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then19 begin20 Result := Reg.ReadString('');
21 Reg.CloseKey;
22 end;
23 end;
24 Reg.Free;
25 end;
Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.php