Search code examples
c#filesystemwatcher

File.Exists() returns false in Deleted event of FileSystemWatcher


I struggled into a problem. I have a synchronization program and I need to know if a watching item is a file or folder. Problem is when I'm checking file or folder exists - no matter what, if I'm checking a file, File.Exists always returns false:

private void onDelete(object o, FileSystemEventArgs e)
{
    if (Directory.Exists(e.FullPath))
        Directory.Delete(watched + e.Name, true);

    if (File.Exists(e.FullPath))
        File.Delete(e.FullPath);
}

Anyone see where the problem could be?


Solution

  • You are trying to find of file/folder on onDelete, this method would be triggered when file / folder is already deleted. So you must not get that file on folder as it is deleted and does not exists.