[Lazarus] Cross platform 'open file with default program'

Juha Manninen juha.manninen at phnet.fi
Sun Jan 24 10:05:38 CET 2010


Hello.

> Under Linux with distros supporting the freedesktop.org standards you
> can execute the 'xdg-open' command. This will use the mime-type and
> the each users preference on that system. This works for URL's too.

Great! For Windows you can use a wrapper for ShellExecute function. I copied 
my code below.
If OSX supports your 'xdg-open' then most environments are covered.

This makes sense only with GUI. Command line progs can't use it.
This should be included in FPC/Lazarus libraries, but which one?

Regards,
Juha Manninen

-------------------------------------

procedure OpenWithAssociatedProg(aFilePath, aFileName: string); overload;
var
  Params: string;
begin
  Params := '';
    if (aFileName <> '') and FileExists(aFilePath + aFileName) then
      // Calls automatically the program associated with the file type.
      ShellExecute(Application.MainForm.Handle, 'open',
                PChar(aFileName),             // FileName
                PChar(Params),                // Parameters
                PChar(aFilePath),             // Current Directory
                SW_SHOWNORMAL);               // ShowCmd
end;

procedure OpenWithAssociatedProg(aFileNameFull: string); overload;
var
  FilePath, FileName: string;
begin
  FilePath := ExtractFilePath(aFileNameFull);
  FileName := ExtractFileName(aFileNameFull);
  OpenWithAssociatedProg(FilePath, FileName);
end;





More information about the Lazarus mailing list