Search code examples
qtqt5.2qfilesystemwatcher

QFileSystemWatcher locks directory on Windows


I am watching a directory recursively using QFileSystemWatcher. And I am not able to rename/delete the parent directory either programmatically or manually if its sub directories are being watched.

When trying to rename manually through system i get a message box saying "The action cannot be completed because the folder/ file in it is opened in another program" and on renaming programmatically it fails.

I got these similar bugs, but no resolution:

http://qt-project.org/forums/viewthread/10530

https://bugreports.qt-project.org/browse/QTBUG-7905

I am not watching . and .. as said in the above link, but still the directory is locked.

In case of programmatically renaming.. I tried a workaround: 1. Remove all the subdirectory paths from watcher before renaming the parent. 2. Rename parent. 3. Add subdirectory paths again.

But here too my program fails on first step. QFileSystemWatcher::removePath() returns false when trying to remove the subdirectory path, and QFileSystemWatcher::directories() show that directory in the paths being watched. Same as posted here https://bugreports.qt-project.org/browse/QTBUG-10846

Since step 1 fails here, step 2 also fails and i cannot rename the parent dir.

I am using Qt5.2.1 and Windows 7.

Kindly help me with a resolution.


Solution

  • This is a bug in QFileSystemWatcher as discussed here

    After days of trying, I am finally able to find the solution of my problem by using Win32 API for watching directories on Windows platform. I wrote a blog post on How to use Win32 Api to monitor directory changes. I would like to share the link so it may help others who land up here to find the solution of the same problem. Win32 API to monitor Directory Changes