Difference between revisions of "FileAssociation"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; fixed typos; added wiki markup)
Line 1: Line 1:
 
{{FileAssociation}}
 
{{FileAssociation}}
  
{{Platform only|Windows|Windows|Windows}}
+
{{Platform only|Windows}}
 +
 
 
== TFileAssociation ==
 
== TFileAssociation ==
  
Line 18: Line 19:
 
== Usage ==
 
== Usage ==
  
First install the package. You can drop the component TFileAssociation (FileAssoc unit) that gets installed in the System tab of the IDE.
+
First install the package. You can then drop the component TFileAssociation (FileAssoc unit) that gets installed in the [[System tab]] of the IDE on your [[TForm|form]].
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
...
 
...
 
uses
 
uses
 
   ...
 
   ...
   FileAssoc;//<-- add fileassociation unit here
+
   FileAssoc; //<-- add fileassociation unit here
  
 
type
 
type
Line 69: Line 70:
 
== How to open the associated file ==
 
== How to open the associated file ==
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
procedure TForm1.FormCreate(Sender: TObject);
 
procedure TForm1.FormCreate(Sender: TObject);
 
var
 
var
Line 92: Line 93:
 
== See also ==
 
== See also ==
  
If this component for some reason doesn't works for you, or you need to run it as administrator for all users without elevating your application, an Inno Setup Script will fit your needs best.
+
If this component for some reason does not work for you, or you need to run it as administrator for all users without elevating your application privileges, an [[Inno_Setup_Usage#File_Association|Inno Setup Script]] may fit your needs best.
 
 
[[Inno_Setup_Usage#File_Association]]
 

Revision as of 03:37, 23 December 2019

Windows logo - 2012.svg

This article applies to Windows only.

See also: Multiplatform Programming Guide

English (en) français (fr)

Windows logo - 2012.svg

This article applies to Windows only.

See also: Multiplatform Programming Guide

TFileAssociation

Author: Lainz

Licence: Modified LGPL

Version: 1.0

Description: This unit registers file association for Windows.

Download

GitHub: https://github.com/lainz/FileAssociation

Usage

First install the package. You can then drop the component TFileAssociation (FileAssoc unit) that gets installed in the System tab of the IDE on your form.

...
uses
  ...
  FileAssoc; //<-- add fileassociation unit here

type

...
    { private declarations }
    assoc: TFileAssociation;
...

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  assoc := TFileAssociation.Create(Self);//<-- create like a regular component
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  assoc.ApplicationName := 'Lazarus IDE';
  assoc.ApplicationDescription := 'RAD for Free Pascal';

  // you can change Extension and Action part for each extension you have

  assoc.Extension := '.lpr';
  assoc.ExtensionName := 'Lazarus Project';
  assoc.ExtensionIcon := '"C:\lazarus\images\lprfile.ico"';

  // full path required, you can use ParamStr(0) to get the path with the .exe name included. The path must be inside quotes if it has whitespace.
  assoc.Action := '"C:\lazarus\lazarus.exe" "%1"'; 
  assoc.ActionName := 'Open';
  assoc.ActionIcon := '"C:\lazarus\images\mainicon.ico"';

  // notice that using RegisterForAllUsers as True requires Administrator Privileges
  // if you want to run without privileges set it to false, but it will register for current user only
  assoc.RegisterForAllUsers:=False;
  if assoc.Execute then
  begin
    ShowMessage('OK');
    assoc.ClearIconCache; //<<-- rebuild icons
  end;
end;

end.

How to open the associated file

procedure TForm1.FormCreate(Sender: TObject);
var
  s: String;
begin
// if there are parameters
  if ParamCount > 0 then
  begin
// load the first parameter
    s := ParamStr(1);
 
// if is a .txt file
    if ExtractFileExt(s) = '.txt' then
    begin
// load the .txt file into a memo
      Memo1.Lines.LoadFromFile(s);
    end;
  end;
end;

See also

If this component for some reason does not work for you, or you need to run it as administrator for all users without elevating your application privileges, an Inno Setup Script may fit your needs best.