0

I'm creating a simple program that manages some files and that program must keep track of file movements made by user.

(I'm not worried at this point about catching all running Windos Explorer's windows and about keeping the program always activated in the tray bar, but I might ask that later in another question)

So, what I need is simply:

  • Launch Windows Explorer Programatically
  • Monitor all file movements, creations and exclusions (made by user inside that window)

Is there an API to use with C#?

P.S: It's a Winforms application, so if there are other ways of doing this, they would be appreciated as well.

Thank you.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214

1 Answers1

2

You can use the FileSystemWatcher

To initialize the FileSystemWatcher:

FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(@"E:\TestDir");
fileSystemWatcher.Changed += OnChanged;
fileSystemWatcher.Created += OnChanged;
fileSystemWatcher.Deleted += OnChanged;
fileSystemWatcher.Renamed += OnChanged;
fileSystemWatcher.EnableRaisingEvents = true;

And the OnChanged-Event:

private static void OnChanged(object sender, FileSystemEventArgs e)
    {
        WatcherChangeTypes watcherChangeTypes = e.ChangeType;
        string fullPath = e.FullPath;
        string name = e.Name;
    }
Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • Still won't deal with how to detect changes made by the user in a Windows explorer window – Adrian Salazar Feb 18 '14 at 11:01
  • @AdrianSalazar look for example at the end of the page of the link – Mujahid Daud Khan Feb 18 '14 at 11:02
  • If you implement the FileSystemWatcher on every logical drive available. Then it would technically detect any change made in the filesystem. And thus also in the Windows Explorer window. There is no other way to detect changes anyway. – woutervs Feb 18 '14 at 11:03
  • @woutervs yest there is, using shell32.dll – Adrian Salazar Feb 18 '14 at 11:04
  • 1
    Yes you are right, but then again one might also reinvent the wheel. He's asking for an API. Not to implement unmanaged code. – woutervs Feb 18 '14 at 11:05
  • That's very good!!! I believe this solves everything better than I expected. I'll just wait a little to see if other answers appear! – Daniel Möller Feb 18 '14 at 11:05
  • 1
    @woutervs and if you had used Sysinternals, you would know how massive is the number of file io events that are triggered constantly in the background. Overkill – Adrian Salazar Feb 18 '14 at 11:07
  • 1
    That is true indeed, and therefore the FileSystemWatcher class has filters. Implementing Shell32.dll to do basically the same what FSW does, would involve a lot of work and testing. And eventually the result would be the same. But indeed it is possible that it might be less CPU intensive. However nowadays with the current processors and memory, the overhead created by FSW can easily be neglected. – woutervs Feb 18 '14 at 11:48
  • I'm only worried about the buffer.....is it enough to watch over an entire disk moving a directory full of files? – Daniel Möller Feb 18 '14 at 16:23