Is there a way to detect when a file is open by another process?
I've tried the following
- Search for handles in processes to get if file is open
This only helps for programs that keep the file open for use. but doesn't work for even simple apps likenotepad.exe
,notepad++
that close the file immediately after loading to memory. - Continuously check for the
Date Accessed
property of the file to know it was opened when it changes.
Doesn't work since it doesn't change when file is opened. eg I opened and read it in notepad but never changed. - Use
FileSystemWatcher
to get event triggered when file is opened
Only can works for events such asChanged
,Created
,Deleted
,Renamed
.... and doesn't detect when file is opened.
So is there any way to detect this? I am thinking it is possible because most antiviruses can sharply detect and block infected when files are opened.