Our application has the ability to add attachments into a table.
Before storing the attachment into the table, the attachment is encoded (EncodeBase64).
The user can see the attachments in a TreeView control.
Double clicking an attachment will:
Decode the attachment (DecodeBase64)
Store the attachment as a file in a certain location.
Open the file by the associated program.
A thread in a timer is running to check if the file is changed (MD5)
When the file is changed the file is encoded again and updated in the table
We need a way to find out when the user done updating/viewing the attachment and closed the attachment in the associated program.
We first thought that opened files are locked by the program and not be able to delete the file but that was a bad assumption.
It works for Word documents, but other files are not locked (e.g. txt-files).
Is there a way to check if a file is still open by the associated program?
I’m struggling with it all the time and it would be great to have some better build-in tools for this. Not all programs lock files when they have it open.
I don’t know for Windows but on OSX I currently shell out to check the file with ‘lsof’.
I’ve submitted <https://xojo.com/issue/43233> a few months ago for it.
As for the MD5 checks, those can get very slow on large files. Maybe the filesize and last modified date/time are sufficient?
If associated programs do not lock the file, then another way is check all opened windows for the Title caption that contains the file name. Example if file name ABC.txt is opened in NotePad, the caption will be ABC.txt - Notepad. If opened in Wordpad, it be ABC.txt - WordPad. I have checked jpg opened in Photo Viewer same behaviour, mp3 and mp4 in VLC player also same.