Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Retrieving the program that is associated with a given extension


How can I retrieve the program that is associated with a given extension?   

    The following example demonstrates retrieving the file associated with a file extension. Example:

    uses
    {$IFDEF WIN32}
      Registry; {We will get it from the registry}
    {$ELSE}
      IniFiles; {We will get it from the win.ini file}
    {$ENDIF}
    {$IFNDEF WIN32}
      const MAX_PATH =  44;
    {$ENDIF}
    function GetProgramAssociation (Ext : string) : string;
    var
    {$IFDEF WIN32}
      reg: TRegistry;
      s : string;
    {$ELSE}
      WinIni : TIniFile;
      WinIniFileName : array[0..MAX_PATH] of char;
      s : string;
    {$ENDIF}
    begin
    {$IFDEF WIN32}
      s := '';
      reg := TRegistry.Create;
      reg.RootKey := HKEY_CLASSES_ROOT;
      if reg.OpenKey('.' + ext + '\shell\open\command',
                     false) <> false then begin
      {The open command has been found}
        s := reg.ReadString('');
        reg.CloseKey;
      end else begin
      {perhaps thier is a system file pointer}
        if reg.OpenKey('.' + ext,
                       false) <> false then begin
          s := reg.ReadString('');
          reg.CloseKey;
          if s <> '' then begin
         {A system file pointer was found}
            if reg.OpenKey(s + '\shell\open\command',
                           false) <> false then
         {The open command has been found}
              s := reg.ReadString('');
            reg.CloseKey;
          end;
        end;
      end;
     {Delete any command line, quotes and spaces}
      if Pos('%', s) > 0 then
        Delete(s, Pos('%', s), length(s));
      if ((length(s) > 0) and
          (s[ ] = '"')) then
        Delete(s,  ,  );
      if ((length(s) > 0) and
          (s[length(s)] = '"')) then
        Delete(s, Length(s),  );
      while ((length(s) > 0) and
             ((s[length(s)] = #32) or
              (s[length(s)] = '"'))) do
        Delete(s, Length(s),  );
    {$ELSE}
      GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName));
      StrCat(WinIniFileName, '\win.ini');
      WinIni := TIniFile.Create(WinIniFileName);
      s := WinIni.ReadString('Extensions',
                              ext,
                              '');
      WinIni.Free;
     {Delete any command line}
      if Pos(' ^', s) > 0 then
        Delete(s, Pos(' ^', s), length(s));
     {$ENDIF}
      result := s;
    end;
    procedure TForm .Button Click(Sender: TObject);
    begin
      ShowMessage(GetProgramAssociation('gif'));
    end;
    

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use