Recent

Author Topic: [SOLVED] Unable to create file associations with FileAssoc  (Read 668 times)

fatmonk

  • Full Member
  • ***
  • Posts: 225
[SOLVED] Unable to create file associations with FileAssoc
« on: December 02, 2019, 04:45:59 pm »
I'm having trouble creating File Associations in Windows using the FileAssoc package.

I installed the package from the Online Package Manager and first of all dropped two of the components onto my form.

I then set various parameters directly in the two components in the object inspector.

The only properties I couldn't figure out how to set in the Object inspector were Action and the icon properties. I want these to use the directory that the application was installed in, so I set these programmatically with:

Code: Pascal  [Select]
  1. FileAssociation_tqr.Action:='"'+Application.ExeName+'" "%1"';
  2. FileAssociation_tqr.ExtensionIcon:=Application.ExeName+',0';

I have an if around the FileAssociation_tqr.Execute and it seems it never succeeds.

I then changed the ExtensionIcon to just be a static icon file in the application dirctory in case finding it in the .exe wasn't working, but no joy. he .Execute still didn't return true.

So I changed tack completely and removed the two components from the form and set about creating them programmatically from scratch. I now have the following, but the Execute still doesn't return true and the association is not made.

Code: Pascal  [Select]
  1. procedure TMainForm.createFileAssociations();
  2. var
  3.   FileAssociation_tqr, FileAssociation_tqt: TFileAssociation;
  4. begin
  5.  
  6.   FileAssociation_tqr:= TFileAssociation.Create(self);
  7.   FileAssociation_tqr.ApplicationName:='My Application Name';
  8.   FileAssociation_tqr.ApplicationDescription:='My Application Description';
  9.  
  10.   FileAssociation_tqr.Extension:='.tqr';
  11.   FileAssociation_tqr.ExtensionName:='My Application File';
  12.   FileAssociation_tqr.ExtensionIcon:=Application.ExeName+',0';
  13. //  FileAssociation_tqr.ExtensionIcon:=ExtractFilePath(Application.ExeName)+'icon.ico';
  14.  
  15.   FileAssociation_tqr.Action:='"'+Application.ExeName+'" "%1"';
  16.   FileAssociation_tqr.ActionName:='Open';
  17.   FileAssociation_tqr.ActionIcon:=Application.ExeName+',0';
  18. //  FileAssociation_tqr.ActionIcon:=ExtractFilePath(Application.ExeName)+'icon.ico';
  19.  
  20.   FileAssociation_tqr.RegisterForAllUsers:=true;
  21.  
  22.   if FileAssociation_tqr.Execute then
  23.    begin
  24.      showMessage('.tqr assoc registered');
  25.      FileAssociation_tqr.ClearIconCache;
  26.    end
  27.   else
  28.    begin
  29.      showMessage('tqr failed');
  30.    end;
  31. end;
  32.  

Where am I going wrong? (I used this as a guide.)

Thanks,

-FM
« Last Edit: December 03, 2019, 10:28:54 am by fatmonk »

lainz

  • Hero Member
  • *****
  • Posts: 3313
    • Lainz
Re: Unable to create file associations with FileAssoc
« Reply #1 on: December 02, 2019, 08:24:10 pm »
If RegisterForAllUsers is true you must run the application as administrator in order to work.

fatmonk

  • Full Member
  • ***
  • Posts: 225
Re: Unable to create file associations with FileAssoc
« Reply #2 on: December 03, 2019, 10:28:37 am »
Hi lainz,

That's got it!

For anyone else that finds this it's worth noting that RegisterForAllUsers has to be explicitly set to false if doing this programmatically - you can't just rely on the default.

-FM