Reputation: 124
We know that in .NET (C# to be specific), we can use a FileSystemWatcher
to detect any type of modification.
public void MessageFileWatcher(string Path, string FileName)
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = Path;
Watcher.Filter = FileName;
Watcher.NotifyFilter = NotifyFilters.LastWrite;
Watcher.Changed += new FileSystemEventHandler(OnChanged);
Watcher.EnableRaisingEvents = true;
}
but I want to keep a watch on a file and after some time also want to delete that file.
To be precise. can FileSystemWatcher
class always look for modifications... and if I want to delete that particular file, will it raise an exception?
Upvotes: 1
Views: 396
Reputation: 25200
It won't raise an exception.
A FileSystemWatcher
doesn't watch files: it watches the file system. In this case you'll find that there will be at least the Deleted event raised when the file is removed.
Upvotes: 2
Reputation: 30882
A FileSystemWatcher
wathes a path with an optional filter, not a single file. Of course, if you set the filter to be the name of the file, the watcher will only watch one file, but that is more of a side-effect that its intended usage.
That said, it's clear that yes, you can delete the file you are watching. However, the deletion should not raise the Changed
event. To monitor the deletion you will need to use the Deleted
event.
Upvotes: 0