Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher locks folder

I have a FileSystemWatcher listening on a folder. When I try renaming this folder (in Windows Explorer) I get a

The action can't be completed because the folder or a file in it is open in another program

error.

Commenting out the FileSystemWatcher calls fixes this.

I've tried reproducing the error with a simple program, but haven't been successful so I'm not sure what's going on here. Has anyone encountered anything like this with a FileSystemWatcher? If so - what might be causing it?

More information:

The following seems to be enough to cause the error:

FileSystemWatcher fsw = new FileSystemWatcher(path);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.EnableRaisingEvents = true;

I can rename files in that folder. And sibling files. I even tried putting a return; as the first line in fsw_Deleted. Still didn't work. As didn't closing Visual Studio, and deleting obj and bin, and running again. And I even tried running the exe file not within VS. Still - no lock if it's not running, Locked if it is.

like image 257
ispiro Avatar asked Jan 29 '15 21:01

ispiro


1 Answers

I've discovered what caused this. There was a second FileSystemWatcher - on a sub directory of the first - which didn't allow renaming the first.

(I'm still surprised, though. A FileSystemWatcher should be "invisible".)

like image 80
ispiro Avatar answered Sep 16 '22 11:09

ispiro