BOSS
BOSS

Reputation: 1898

C# : filesystemwatcher class

i have a question about the moving file event on filesystemwatcher class , i`d like to stop the moving of file or edit it when the moving file event arises for a certain file , is that possible to handle inside the moving event ?

Upvotes: 0

Views: 221

Answers (3)

CodeCaster
CodeCaster

Reputation: 151588

You only receive the events after the fact has happened. It's a mere notification, not an event you'd have to approve. This can also be guessed from the missing Cancel or Handled property in the FileSystemEventArgs (as opposed to, for example, the KeyEventArgs) class.

You can detect a move and try to move the file back, based on the OldFullPath property of the RenamedEventArgs you receive.

This might however be confusing to your users or to other software. And try not to end up in an infinite loop, where you move the file back and forth every time you receive the event.

Upvotes: 1

Ethan Cabiac
Ethan Cabiac

Reputation: 4993

You cannot directly "cancel" the operation by means of the event handler. You would have to provide a compensating operation to programatically "undo" any changes you want undone.

Upvotes: 2

samjudson
samjudson

Reputation: 56853

No, there is no way to stop someone moving or renaming a file using the FileSystemWatcher class.

If you look, none of the event arguments passed by the events on the FileSystemWatcher class have a Cancel property. Also, the fact that the class is simply called a Watcher is a bit of a clue.

You might consider using Access Control Lists to make sure someone cannot delete a file (since a move is really just a copy/delete). Or perhaps you could try opening a FileStream on the file so that you have it locked.

Upvotes: 2

Related Questions