Open file dialog filter by file contents
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?
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.
- 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.
- 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.
- 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?