4

My FileSystemWatcher isn't throwing any events. I've looked at these similar questions, none seem to be an answer for my problem:

*Edit: My goal is to capture when an XLS file is copied to or created in a directory.

Monitor class:

public class Monitor
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    readonly string bookedPath = @"\\SomeServer\SomeFolder\";

    public delegate void FileDroppedEvent(string FullPath);
    public event FileDroppedEvent FileDropped;

    public delegate void ErrorEvent(Exception ex);
    public event ErrorEvent Error;

    public Monitor()
    {
        watcher.Path = bookedPath;
        watcher.Filter = "*.xls";
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        watcher.Error += new ErrorEventHandler(watcher_Error);
    }

    void watcher_Error(object sender, ErrorEventArgs e)
    {
        Error(e.GetException());
    }

    void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        if (e.ChangeType != WatcherChangeTypes.Created) return;
        FileDropped(e.FullPath);
    }

    public void Start()
    {
        watcher.EnableRaisingEvents = true;
    }

    public void Stop()
    {
        watcher.EnableRaisingEvents = false;
    }
}

Simple form with Listbox:

public partial class Form1 : Form
{
    Monitor monitor = new Monitor();

    public Form1()
    {
        InitializeComponent();
        FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        Load += new EventHandler(Form1_Load);
        monitor.FileDropped += new Monitor.FileDroppedEvent(monitor_FileDropped);
        monitor.Error += new Monitor.ErrorEvent(monitor_Error);
    }

    void Form1_Load(object sender, EventArgs e)
    {
        monitor.Start();
    }

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        monitor.Stop();
    }

    void monitor_Error(Exception ex)
    {
        listBox1.Items.Add(ex.Message);
    }

    void monitor_FileDropped(string FullPath)
    {
        listBox1.Items.Add(FullPath);
    }
}

What am I doing wrong?

Community
  • 1
  • 1
DontFretBrett
  • 1,135
  • 3
  • 17
  • 32
  • Does the user the code is running as have access to the network path? – adrianbanks May 22 '14 at 22:03
  • Yes. I'm running it and I have access – DontFretBrett May 22 '14 at 22:04
  • You might find the following thread useful: http://stackoverflow.com/questions/11219373/filesystemwatcher-to-watch-unc-path – Edin May 22 '14 at 22:07
  • 1
    Does it work with a non-network path? – Cameron May 22 '14 at 22:07
  • You're only looking for `XLS` files and only monitoring when the file name changes. Is this correct or did you inadvertently limit your filters in testing? If you want to catch events other than when the file name changes, declare more `NotifyFilter`s – TyCobb May 22 '14 at 22:07
  • I'll test on a non-network path. But I do need to get it to work on the network path eventually, even if I don't use FileSystemWatcher. TyCobb, originally I had LastAccess not FileName, I made the edit, but you must have got in before I edited it. At any rate, I tried renaming an XLS file and it didn't fire anyway. – DontFretBrett May 22 '14 at 22:08
  • Cameron, comfirmed it's not firing on a local path either – DontFretBrett May 22 '14 at 22:10
  • I think you need to add NotifyFilters.LastWrite or NotifyFilters.CreationTime – Edin May 22 '14 at 22:11
  • Put a breakpoint on `watcher_Changed`. I think it may be returning straight away because of `if (e.ChangeType != WatcherChangeTypes.Created) return;` – PeteGO May 22 '14 at 22:13
  • Edin, good point. I tried LastWrite, copied a file over. No events. CodeCaster, I'm not sure where the problem is. I was trying to be helpful by providing the details. If you don't want to follow the method calls you don't have to. PeteGo - tried that, doesn't fire – DontFretBrett May 22 '14 at 22:13
  • Have you tried `NotifyFilters.FileName`? – PeteGO May 22 '14 at 22:15
  • Yes I tried that PeteGO, someone above said that only fires when a filename is changed. – DontFretBrett May 22 '14 at 22:16
  • What's your specific goal? – Derek W May 22 '14 at 22:16
  • Derek W, I want an event to fire when an XLS file is copied to a directory. I'll add that to my original post – DontFretBrett May 22 '14 at 22:16
  • Listen for all events and see what is fired. Or start with [process monitor](http://technet.microsoft.com/en-us/sysinternals/bb896645) and see what events are actually fired when operation you are interested in happens. Often simple thing like "create" are not that simple (i.e. create "foo.~temp" and rename to "foo.realExt") – Alexei Levenkov May 22 '14 at 22:33
  • The very first event that should fire when an xls file is created in the directory is watcher_Changed, I set a breakpoint on that and it never happens. I'm 100% confident the form_load fires (tested with a breakpoint), stepped through the Start(), the code fires sets EnableRaisingEvents to true. – DontFretBrett May 22 '14 at 22:35
  • Why do you think the Changed event would fire, not the Created event? – LarsTech May 22 '14 at 22:49
  • even why i try both, it never fires: watcher.Created += new FileSystemEventHandler(watcher_Changed); watcher.Changed += new FileSystemEventHandler(watcher_Changed); – DontFretBrett May 22 '14 at 22:57

4 Answers4

2

Try this out. Works for me for a very similar task.

watcher.NotifyFilter = NotifyFilters.FileName;   
watcher.Created += new FileSystemEventHandler(handler);     
watcher.Renamed += new RenamedEventHandler(handler);
Derek W
  • 9,708
  • 5
  • 58
  • 67
  • Tried that out, copied a file into the directory. Nothing fired off. I set a breakpoint in the event of the watcher_change event too so it's not an issue with my custom events. Thanks though – DontFretBrett May 22 '14 at 22:24
0

This may be because the file metadata hasn't been updated yet. This may happen if you are continuously writing to the file.

Daniel L.
  • 437
  • 3
  • 15
0

Have you tried the following:

watcher.Path = directory name;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xls";

watcher.Changed += OnDirectoryChange;
watcher.Error += OnError;
watcher.EnableRaisingEvents = true;

// Watch only files not subdirectories.
watcher.IncludeSubdirectories = false;
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
-1

Your issue is with the filters as well as your events I believe. NotifyFilters.LastAccess will only trigger when a file is opened. Try using:

NotifyFilters.LastWrite | NotifyFilters.CreationTime

This will watch for written/created files. Next, hook up to the Created delegate to handle newly created files:

watcher.Created += YourDelegateToHandleCreatedFiles

The way FileSystemWatcher works is to first use the NotifyFilters to limit the event triggers. Then, you use the actual events to do the work. By hooking into the Created event you'll only do work when a file is created.

Haney
  • 32,775
  • 8
  • 59
  • 68