Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file dialog filter by file contents

Tags:

delphi

vcl

I typically use TOpenDialog with its Filter property for narrowing down available files for the user. I would like to further filter these files down by their content.

For example, the open file dialog should display only files with a ".bin" filename extension and its first 4 magic bytes are 0x7F, 0x45, 0x4C, 0x46 (ELF executable).

The condition will not always necessarily be identifying certain executable formats, but other binary data as well.

Is there a standard way of doing this in Delphi/VCL or do I have to postpone the content inspection until after the user has selected the file?

like image 902
ardnew Avatar asked Nov 16 '25 21:11

ardnew


1 Answers

There's no way to achieve what you want. The system file dialog does not allow you to filter the files that it displays based on their content.

The OnIncludeItem event tantalisingly appears to do what you need. However, it has the following problems.

  1. The event wraps the CDN_INCLUDEITEM notification. This notification is fired by the legacy XP common dialog boxes, but not by the modern common item dialog. So, using the event forces the legacy XP common dialog boxes onto your program. If for no other reason, this has cosmetic downsides.
  2. More significantly, handling CDN_INCLUDEITEM has no impact on files. It only influences non-filesystem shell objects. Form the documentation:

The dialog box always includes items that have both the SFGAO_FILESYSTEM and SFGAO_FILESYSANCESTOR attributes, regardless of the value returned by CDN_INCLUDEITEM.

Related questions:

  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • In Delphi how does the OnIncludeItem event work on TOpenDialog?
like image 159
David Heffernan Avatar answered Nov 18 '25 20:11

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!