1. Home
  2. Computing & Technology
  3. Delphi Programming

Get Associated application by Extension

By Zarko Gajic, About.com

Idea:
Do a search in the Registry for your extension in HKEY_CLASSES_ROOT, after finding it get its default value and then do a new search for it. Now, read the Shell\Open\Command key's default value.

~~~~~~~~~~~~~~~~~~~~~~~~~
{
Usage: //find and start
var
   sExe:string;
begin
   sEXE:=GetExeByExtension('.htm') ;
   if (sEXE <> '') and FileExists(sEXE) then
     ShellExecute(Handle,'OPEN', PChar(sEXE), nil, nil, SW_SHOWNORMAL) ;
}

Uses Registry;

function GetExeByExtension(sExt : string) : string;
var
   sExtDesc:string;
begin
   with TRegistry.Create do
   begin
     try
       RootKey:=HKEY_CLASSES_ROOT;
       if OpenKeyReadOnly(sExt) then
       begin
         sExtDesc:=ReadString('') ;
         CloseKey;
       end;
       if sExtDesc <>'' then
       begin
         if OpenKeyReadOnly(sExtDesc + '\Shell\Open\Command') then
         begin
           Result:= ReadString('') ;
         end
       end;
     finally
       Free;
     end;
   end;

   if Result <> '' then
   begin
     if Result[1] = '"' then
     begin
       Result:=Copy(Result,2,-1 + Pos('"',Copy(Result,2,MaxINt))) ;
     end
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Using an object with no reference variable
« Disable LogOff, TaskManager and ShutDown on Win NT systems (2000/XP)

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2003 Delphi Tips
  7. Get Associated application by Extension

©2009 About.com, a part of The New York Times Company.

All rights reserved.