FileAssociation: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: FileAssociation
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## TFileAssociation | |
| 2 | + | ||
| 3 | + | Author: Lainz | |
| 4 | + | ||
| 5 | + | Licence: Modified LGPL | |
| 6 | + | ||
| 7 | + | Version: 1.0 | |
| 8 | + | ||
| 9 | + | Description: This unit registers file association for Windows. | |
| 10 | + | ||
| 11 | + | ## Download | |
| 12 | + | ||
| 13 | + | GitHub: <https://github.com/lainz/FileAssociation> | |
| 14 | + | ||
| 15 | + | ## Usage | |
| 16 | + | ||
| 17 | + | 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]]. | |
| 18 | + | ||
| 19 | + | All parameters are mandatory. Especially ActionName which must be 'Open' to work with double click. This is useful as well for default commands like 'Edit' and 'Print'. This must be in English for 'Edit', 'Open' and 'Print' so it can access the right registry entry. You can customize the translation with ActionText. | |
| 20 | + | ||
| 21 | + | ``` pascal | |
| 22 | + | ... | |
| 23 | + | uses | |
| 24 | + | ... | |
| 25 | + | FileAssoc; //<-- add fileassociation unit here | |
| 26 | + | ||
| 27 | + | type | |
| 28 | + | ||
| 29 | + | ... | |
| 30 | + | { private declarations } | |
| 31 | + | assoc: TFileAssociation; | |
| 32 | + | ... | |
| 33 | + | ||
| 34 | + | procedure TfrmMain.FormCreate(Sender: TObject); | |
| 35 | + | begin | |
| 36 | + | assoc := TFileAssociation.Create(Self);//<-- create like a regular component | |
| 37 | + | end; | |
| 38 | + | ||
| 39 | + | procedure TfrmMain.Button1Click(Sender: TObject); | |
| 40 | + | begin | |
| 41 | + | assoc.ApplicationName := 'Lazarus IDE'; | |
| 42 | + | assoc.ApplicationDescription := 'RAD for Free Pascal'; | |
| 43 | + | ||
| 44 | + | // you can change Extension and Action part for each extension you have | |
| 45 | + | ||
| 46 | + | assoc.Extension := '.lpr'; | |
| 47 | + | assoc.ExtensionName := 'Lazarus Project'; | |
| 48 | + | assoc.ExtensionIcon := '"C:\lazarus\images\lprfile.ico"'; | |
| 49 | + | ||
| 50 | + | // 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. | |
| 51 | + | assoc.Action := '"C:\lazarus\lazarus.exe" "%1"'; | |
| 52 | + | assoc.ActionName := 'Open'; | |
| 53 | + | assoc.ActionIcon := '"C:\lazarus\images\mainicon.ico"'; | |
| 54 | + | ||
| 55 | + | // notice that using RegisterForAllUsers as True requires Administrator Privileges | |
| 56 | + | // if you want to run without privileges set it to false, but it will register for current user only | |
| 57 | + | assoc.RegisterForAllUsers:=False; | |
| 58 | + | if assoc.Execute then | |
| 59 | + | begin | |
| 60 | + | ShowMessage('OK'); | |
| 61 | + | assoc.ClearIconCache; //<<-- rebuild icons | |
| 62 | + | end; | |
| 63 | + | end; | |
| 64 | + | ||
| 65 | + | end. | |
| 66 | + | ``` | |
| 67 | + | ||
| 68 | + | ## How to open the associated file | |
| 69 | + | ||
| 70 | + | ``` pascal | |
| 71 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 72 | + | var | |
| 73 | + | s: String; | |
| 74 | + | begin | |
| 75 | + | // if there are parameters | |
| 76 | + | if ParamCount > 0 then | |
| 77 | + | begin | |
| 78 | + | // load the first parameter | |
| 79 | + | s := ParamStr(1); | |
| 80 | + | ||
| 81 | + | // if is a .txt file | |
| 82 | + | if ExtractFileExt(s) = '.txt' then | |
| 83 | + | begin | |
| 84 | + | // load the .txt file into a memo | |
| 85 | + | Memo1.Lines.LoadFromFile(s); | |
| 86 | + | end; | |
| 87 | + | end; | |
| 88 | + | end; | |
| 89 | + | ``` | |
| 90 | + | ||
| 91 | + | ## Another way of registering file associations | |
| 92 | + | ||
| 93 | + | Without using the component is as well possible to register a file association. This however doesn't cover adding the application to Default Programs, but is shorter if you only want to register a file type that's owned by your application. | |
| 94 | + | ||
| 95 | + | ``` pascal | |
| 96 | + | {$IFDEF WINDOWS} | |
| 97 | + | uses Registry, ShlObj; | |
| 98 | + | ||
| 99 | + | procedure TMainForm.RegisterFileType(ExtName: string; AppName: string); | |
| 100 | + | var | |
| 101 | + | reg: TRegistry; | |
| 102 | + | begin | |
| 103 | + | reg := TRegistry.Create; | |
| 104 | + | try | |
| 105 | + | reg.RootKey := HKEY_CLASSES_ROOT; | |
| 106 | + | reg.OpenKey('.' + ExtName, True); | |
| 107 | + | reg.WriteString('', ExtName + 'file'); | |
| 108 | + | reg.CloseKey; | |
| 109 | + | reg.CreateKey(ExtName + 'file'); | |
| 110 | + | reg.OpenKey(ExtName + 'file\DefaultIcon', True); | |
| 111 | + | reg.WriteString('', AppName + ',0'); | |
| 112 | + | reg.CloseKey; | |
| 113 | + | reg.OpenKey(ExtName + 'file\shell\open\command', True); | |
| 114 | + | reg.WriteString('', AppName + ' "%1"'); | |
| 115 | + | reg.CloseKey; | |
| 116 | + | finally | |
| 117 | + | reg.Free; | |
| 118 | + | end; | |
| 119 | + | SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil); | |
| 120 | + | end; | |
| 121 | + | {$ENDIF} | |
| 122 | + | ``` | |
| 123 | + | ||
| 124 | + | ``` pascal | |
| 125 | + | var | |
| 126 | + | reg: TRegistry; | |
| 127 | + | ||
| 128 | + | begin | |
| 129 | + | {$IFDEF WINDOWS} | |
| 130 | + | reg := TRegistry.Create; | |
| 131 | + | try | |
| 132 | + | reg.RootKey := HKEY_CLASSES_ROOT; | |
| 133 | + | if not reg.KeyExists('ext' + 'file\shell\open\command') then | |
| 134 | + | RegisterFileType('ext', ExtractFilePath(Application.ExeName) + 'my_app.exe'); | |
| 135 | + | finally | |
| 136 | + | reg.Free; | |
| 137 | + | end; | |
| 138 | + | ||
| 139 | + | if ParamCount > 0 then | |
| 140 | + | begin | |
| 141 | + | s := ParamStr(1); | |
| 142 | + | if ExtractFileExt(s) = '.ext' then | |
| 143 | + | LoadFile(s); | |
| 144 | + | end; | |
| 145 | + | {$ENDIF} | |
| 146 | + | ``` | |
| 147 | + | ||
| 148 | + | ## See also | |
| 149 | + | ||
| 150 | + | 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|Inno Setup Script]] may fit your needs best. | |
| 151 | + | ||
| 152 | + | --- | |
| 153 | + | *From the Free Pascal wiki, page "FileAssociation", as it stood on 2020-02-15 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
