Is there a way to determine with the FSW if a file or a directory has been deleted?
-
I'm not sure that you can, frankly. The obvious workaround would be to recurse on startup to build a list of directories. If it's not in that list, it's was a file. – Steven Sudit Jul 26 '10 at 16:23
-
4It's not the "C# FileSystemWatcher". It's the .NET FileSystemWatcher. It works just as well with all .NET languages, not just C#. – John Saunders Jul 26 '10 at 17:50
-
1It occurs to me that it might work if you ran two separate FSW's monitoring the same directory and differing only in their `NotifyFiter` values. – Steven Sudit Jul 26 '10 at 17:59
-
Steven Sudit I think you misunderstood NotifyFilter – Ini Aug 18 '19 at 02:48
4 Answers
Here's a simplified and corrected version of fletcher's solution:
namespace Watcher
{
class Program
{
private const string Directory = @"C:\Temp";
private static FileSystemWatcher _fileWatcher;
private static FileSystemWatcher _dirWatcher;
static void Main(string[] args)
{
_fileWatcher = new FileSystemWatcher(Directory);
_fileWatcher.IncludeSubdirectories = true;
_fileWatcher.NotifyFilter = NotifyFilters.FileName;
_fileWatcher.EnableRaisingEvents = true;
_fileWatcher.Deleted += WatcherActivity;
_dirWatcher = new FileSystemWatcher(Directory);
_dirWatcher.IncludeSubdirectories = true;
_dirWatcher.NotifyFilter = NotifyFilters.DirectoryName;
_dirWatcher.EnableRaisingEvents = true;
_dirWatcher.Deleted += WatcherActivity;
Console.ReadLine();
}
static void WatcherActivity(object sender, FileSystemEventArgs e)
{
if(sender == _dirWatcher)
{
Console.WriteLine("Directory:{0}",e.FullPath);
}
else
{
Console.WriteLine("File:{0}",e.FullPath);
}
}
}
}

- 19,391
- 1
- 51
- 53
-
I got the bugs out of it, but you did come up with the idea before I did. – Steven Sudit Jul 26 '10 at 18:24
-
4This solution does not take into consideration that both `FileSystemWatcher`s are running on separate threads. Thus, if there are a lot of events in the watched directory, the order of events that you see in your app can be different - ie. you can record that a file was created in a directory that doesn't exist yet, followed by an event that creates said directory. – Paya Jan 17 '15 at 00:00
-
Similar to what Paya says: with one watcher, in case of a delete of a directory, you will first get delete events for the files and finally the delete event for the directory. With two watchers again the order is not guaranteed and you might get in (less) trouble when you want to replicate these events... – mvermand Apr 29 '15 at 19:57
-
FileName NotifyFilter doesn't include changed files, just new files or renamed files. I'm almost at the point of decompiling to write a better FileSystemWatcher class. This is ridiculous and highly lacking in performance. – Barry Dec 03 '16 at 22:16
I temporary use the "Path" function initially, but later in case of not delete I fix it by Directory.Exists. However that doesn't fix the Delete case
bool isDirectory = Path.GetExtension(e.FullPath) == string.Empty;
if (e.ChangeType != WatcherChangeTypes.Deleted)
{
isDirectory = Directory.Exists(e.FullPath);
}

- 4,539
- 1
- 29
- 31
Your question only makes sense if there could be a file and a directory with the same name at the same path. e.g. If you have filenames without extension or directories with extension.
If your directories and files follow the usual conventions, just checking for the presence of an extension in the full path(bool iSDirectory = Path.GetExtension(e.FullPath).Equals("");
), which works whether the file/directory exists or not, because the method just parses the path given and has no connection to the file whatsoever.
If you have to deal with the non-conventional issues I mentioned in the beginning, you could check whether a directory or a file exists at that location. If neither does, you treat them as if both were deleted. If one of them does exist, you treat the other as if it was deleted.
Your inquiry implies that you keep a list of the files and directories somewhere, so, checking against that list, you can make your decision about handling.
I think that this approach is better than the solution given that uses two filesystem watchers in order to tell the difference.

- 2,297
- 29
- 20
You could interrogate the FileSystemEventArgs.FullPath
property to tell if it is a directory or a file.
if (Path.GetFileName(e.FullPath) == String.Empty)
{
//it's a directory.
}
To check if it is a file or directory.

- 67,283
- 14
- 105
- 142
-
3
-
@Steven Sudit: You'd have to check the FileSystemEventArgs.ChangeType property first then. – Yuriy Faktorovich Jul 26 '10 at 16:19
-
2`File.GetAttributes` throws a `FileNotFoundException` for a deleted file. – Phil Gan Jul 26 '10 at 16:21
-
@Yuriy: That's not going to help any. http://msdn.microsoft.com/en-us/library/t6xf43e0.aspx – Steven Sudit Jul 26 '10 at 16:23
-
@Yuriy: I don't think this works. The `Path.GetFileName` has no crystal ball: it just manipulated text. – Steven Sudit Jul 26 '10 at 16:36
-
@Steven Sudit: Can you think of a case where it would not work, the documentation is specifically *If the last character of path is a directory or volume separator character, this method returns String.Empty*. Especially since FullPath *Gets the fully qualifed path of the affected file or directory*. – Yuriy Faktorovich Jul 26 '10 at 16:38
-
This expression doesn't evaluate to true for either a folder or a file. – Phil Gan Jul 26 '10 at 16:49
-
@Yuriy: You're making the false assumption that deleting a folder named "C:\abc\def" will fill `e.FullPath` with "C:\abc\def\". This is *not* the case. – Steven Sudit Jul 26 '10 at 16:58
-
@Steven Sudit: Why not? "C:\abc\def" would not be the fully qualified path, and FullPath returns a fully qualified one. – Yuriy Faktorovich Jul 26 '10 at 17:06
-
2@Yuriy: As I've been saying, this turns out not to be the case. Rather than trust your interpretation of the documentation or my memory, I just tested it. I set a FSW on "C:\abc" and created a "def" subdirectory. I found that `e.FullPath` contained "C:\abc\def". Note the lack of any trailing backslash. – Steven Sudit Jul 26 '10 at 17:56
-
1Do me a favor: please don't delete this answer. I think it's very educational. – Steven Sudit Jul 26 '10 at 18:25
-
-
@Yuriy: Yes, please do. We could all benefit from a bit less reliance on authority and a bit more reliance on experimentation. – Steven Sudit Jul 26 '10 at 18:47
-
@yuriy, I created a FSW pointed at a folder copied your code into the delete handler and then created and deleted folders and text files. The handler was fired, but the expression evaluated to false for both files and folders. – Phil Gan Jul 27 '10 at 08:02
-
1I just tried it and when I tried to delete a folder its returns the folder name normally not empty – Bishoy Hanna Mar 24 '14 at 06:01
-
1Steven Sudit - this is not educational at all. will fail is the file has no extension – Itay Gal Aug 11 '16 at 12:23