Recent

Author Topic: Check if file association already exists  (Read 2565 times)

fatmonk

  • Full Member
  • ***
  • Posts: 222
Check if file association already exists
« on: December 02, 2019, 03:56:26 pm »
I'm using the FileAssoc package to register a couple of file associations for my application, but rather than setting them blindly every time the application is run, I'd like to check whether the file association already exists beforehand.

Is there a way to check?

(I'm building on Windows).

Thanks,

-FM

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: Check if file association already exists
« Reply #1 on: December 02, 2019, 04:07:16 pm »
Check the Registry?
IIRC it's in HKEY_CLASSES_ROOT
e.g.:
You search in HKCR for ".jpg"
You'll find an entry like "(Standard) = jpegfile"
Now search for "jpegfile"
You'll find something like [HKEY_CLASSES_ROOT\jpegfile]
And if that fileextension is associated with a Program, you'll find a Subkey command
[HKEY_CLASSES_ROOT\jpegfile\shell\open\command]
And there a Key called "(Standard)" which defines the executable it's associated with

If Keyexists Then DontDoAnyThing Else RegisterWithMyProgram
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

fatmonk

  • Full Member
  • ***
  • Posts: 222
Re: Check if file association already exists
« Reply #2 on: December 02, 2019, 04:33:16 pm »
I originally started off down the registry route as I was looking to manually create the  registry entries for the association myself, but the trail of registry keys looked very convoluted.

Using FileAssoc seems like a way to create the associations without all of the manual work BUT it seems to be failing to register them at the moment and I can't figure out why :-(

I was hoping for something a bit less manual than following along through various registry keys, but maybe I'll just have to write a fuinction to do exactly that.

-FM

jamie

  • Hero Member
  • *****
  • Posts: 2162
Re: Check if file association already exists
« Reply #3 on: December 03, 2019, 12:06:17 am »
[urlhttps://forum.lazarus.freepascal.org/index.php?topic=1709.0][/url]

Maybe this is what you need.. Its windows so take notice, I didn't see what you are using for OS.
Number 1 at blue screen app creations!

fatmonk

  • Full Member
  • ***
  • Posts: 222
Re: Check if file association already exists
« Reply #4 on: December 03, 2019, 12:23:23 am »
Thanks Jamie,

Is that the link you meant to post as that one is about extracting path names from .link files rather than file associations.

Thanks,

FM

jamie

  • Hero Member
  • *****
  • Posts: 2162
Re: Check if file association already exists
« Reply #5 on: December 03, 2019, 12:25:02 am »
oh, Sorry,,, I'll need to look again.

https://perevoznyk.wordpress.com/2011/05/19/delphi-class-for-managing-file-associations/

You should be able to make the code work in Lazarus.
« Last Edit: December 03, 2019, 12:41:11 am by jamie »
Number 1 at blue screen app creations!

lainz

  • Hero Member
  • *****
  • Posts: 3307
    • Lainz
Re: Check if file association already exists
« Reply #6 on: December 03, 2019, 01:05:30 am »
Using FileAssoc seems like a way to create the associations without all of the manual work BUT it seems to be failing to register them at the moment and I can't figure out why :-(

You need to run as administrator if the association is for all users. If it's for a single user, normal privileges just works ( RegisterForAllUsers:=false; )

Remember that now on Windows 10 you can not simply change a file association previously set by the user, that means the user will see a dialog prompting with which application you want to open the file (if more than one is available).

For example if I install LazPaint and mark all file associations, when I open a .bmp file, Windows will prompt me if I want to use LazPaint or anything else. That is a security feature.

jamie

  • Hero Member
  • *****
  • Posts: 2162
Re: Check if file association already exists
« Reply #7 on: December 03, 2019, 01:54:10 am »
I took that Delphi example and converted it, It's not 100% because there is a function I can not seem to duplicate or use an alternate, it is some C++ variant of the function. I guess one could be made or one may already exist in the libs of fpc but its on line #190 and I commented it out so I can get a min example working..

Attached is the complete project with unit file, someone can fix the rest of it  >:(
Number 1 at blue screen app creations!

fatmonk

  • Full Member
  • ***
  • Posts: 222
Re: Check if file association already exists
« Reply #8 on: December 06, 2019, 05:27:14 pm »
@lainz: I didn't realise that with Win10... that pretty much means that checking for the association is less necessary. The overhead of setting it every time is probably the same as checking it every time, so I'll just leave it in there to run every time.

@jamie: Thanks for that. I'll take a look, but in this instance I think getting it working is probably more trouble than it's worth given what lainz said.

Thanks,

Dan