2

I'm using System.IO.FileSystemWatcher to monitor a directory and does things when a file or directory is modified or deleted.

The issue I have is that once a folder is deleted, the Deleted event is raised, but the argument I get is not useful enough for me to know if a directory or file is deleted.

The argument I get is a path, for example C:\Temp\a which can be a directory or a file with no extension.

I tried the followings, but they all failed to give me the correct result.

var c = Path.DirectorySeparatorChar.ToString();
var isDir = Path.GetDirectoryName(path) == path.TrimEnd(c.ToCharArray());

and

var isDir =(File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
  • The folder or file would have been deleted by the time you received an event. What you could do is keep a list of all files and folders internally and match the path received from your event against that list – Tien Dinh Dec 26 '14 at 22:10
  • If it was a file, the base directory would still exist? – TheGeneral Dec 26 '14 at 22:34
  • @Saruman, if it was a directory, the base directory would still exist too. – Ray Cheng Dec 26 '14 at 22:44

3 Answers3

3

I guess you can't reliably do that.

A path to a file and a directory can be ambiguous: a directory can have an 'extension' and a file can have no extension at all.

If a file or directory exists, the OS knows what it is. When it's gone, no one knows any more.

You have to remember whether it is a file or directory before deletion, not trying to determine it afterwards.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • You are correct, it can be known by just looking at a string, but I hope there's a OS level command I can use for that purpose since delete is almost never really deleting anything. Else, the data recovery companies will be out of business. I tried http://stackoverflow.com/a/1395251/310767 but it gives me the wrong result. – Ray Cheng Dec 26 '14 at 22:18
  • You have to search the ntfs disk format to find it. Seems to much work to for something like this if you can retrieve it upfront. – Patrick Hofman Dec 26 '14 at 22:23
  • 1
    For what the OS is concerned, the path is gone, so no info is available after the deletion. – John Dec 26 '14 at 22:25
  • @cod: I totally agree with you. In fact, for a soft deletion, the file is moved to some system directory. So the original path should be kept somewhere, not sure if it is accessible. – Patrick Hofman Dec 26 '14 at 22:30
  • 1
    @Ray: why not have two watchers? One for files, one for directories... – Patrick Hofman Dec 26 '14 at 22:31
  • @PatrickHofman, how to create two watchers? They have to be able to watch sub directories and files also. – Ray Cheng Dec 26 '14 at 22:45
  • See the duplicate question. Use `NotifyFilter = NotifyFilters.FileName` and `DirectoryName`. – Patrick Hofman Dec 27 '14 at 09:36
1

You can store the files and directories into separate collections before starting watching. Then whenever something is deleted just check which collection holds the deleted path.

For example:

var files = Directory.GetFiles("path", "*", SearchOption.AllDirectories);
var directories = Directory.GetDirectories("path","*", SearchOption.AllDirectories);

private static void OnDeleted(object source, FileSystemEventArgs e)
{
    if(files.Contains(e.FullPath))
    {
        // it's a file
    }
    else 
    {
       // it's a directory
    }
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

In case, you have files which have extensions and Directories which have No Extension. I will suggest to use following simple solution

private static void OnDeleted(object source, FileSystemEventArgs e)
{
  bool iSDirectory = Path.GetExtension(e.FullPath).Equals("");
  if(iSDirectory)
   {
        Console.WriteLine("Directory:{0}",e.FullPath);
   }
   else
   {
         Console.WriteLine("File:{0}",e.FullPath);
   }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Vikrant
  • 1,149
  • 12
  • 19