-1

FileSystemWatcher is not working on a file that is being written to by another program. Every time this file is written to, a new line is added. I want to know when this occurs. I have tried to implement the FileSystemWatcher, however, it does not alert me every time a change occurs. I know when the file has been written to, so I know that the watcher has missed alerting me. I created a button to check the file size. When the button is clicked (after knowing the file has been written to) the alert finally triggers. I added an Error to the watcher to try and check for the buffer being full, but that does not appear to be the case. I think it has something to do with the fact the file is still able to be written to by the other program. Any advice would be appreciated. CreateFileWatcher is ran when the program is started. The path shown is not the true path. I just want to be alerted when the file has been written to.

 public void CreateFileWatcher()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:\Program Files\test";
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
        watcher.Filter = "";

        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Error += new ErrorEventHandler(LogBufferError);

        watcher.EnableRaisingEvents = true;
    }

 private void OnChanged(object sender, FileSystemEventArgs e)
    {
        MessageBox.Show("SomethingChanged");
    }

    void LogBufferError(object sender, ErrorEventArgs e)
    {
        string log = string.Format("{0:G} | Buffer limit exceeded", DateTime.Now);
        MessageBox.Show("BufferError");
    }
adventuresncode
  • 103
  • 1
  • 11
  • 1
    Does this help: https://stackoverflow.com/questions/3540801/filesystemwatcher-does-not-report-changes-in-a-locked-file – Klaus Gütter Nov 14 '18 at 19:30
  • @KlausGütter That is a good reference. It looks like they used a separate thread though to read the file to get the changed event to trigger. I believe that separate thread would have to be on a loop. I assume it would just be easier to create a loop that checks for file size changes every n seconds – adventuresncode Nov 14 '18 at 19:44
  • 1
    Have you tried watching a file that you have written to, leaving it open, and then also closing it to see how FSW responds? I would think that FSW didn't respond every time a file was written to unless that file was closed. That seems to be most logical. If you need to monitor the files for when they are modified then this may happen even while it's being written to and that sounds like a lot of overhead. – Michael Puckett II Nov 14 '18 at 20:22
  • I also agree with you in that it may be better to do it manually if that's what you must do. I do not agree with the answer in the link provided in the first comment. If you have to force the FSW to trigger a change by manipulating it on your end then you definitely should just be doing the work yourself and ignoring it altogether. – Michael Puckett II Nov 14 '18 at 20:24

1 Answers1

0

Your filter needs to be:

watcher.Filter = "*.*";
Clayton Harbich
  • 505
  • 1
  • 7
  • 16