none
File SystemWatcher RRS feed

  • Question

  • Greetings,

    I have a C# Windows Forms app that has been running for a couple of years.  One of its components watches a log file (text) which is periodically written to by a machine to which the computer is attached.  It has been faithfully recording that information for over two years until just a couple of days ago.

    After trying all of the usual steps (rebooting, checking connections, etc.) I began testing the program in Visual Studio and have determined that the File System Watcher object was not firing when the log file had a new line of text added to it.  I then created a simple C# project (below) to do the same thing and it exhibited exactly the same behavior. I compiled it with both .net 4.0 and 4.5.1.

    When I made a copy of the log file in the same directory it would trigger the event handler, so I know the File System Watcher was running.

    Why isn't this firing when the file has a new line of text added to it?  What has changed?

    I look forward to your input.

    Here is the code;

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace FileSystemWatcherTestTwo
    {
        public partial class Form1 : Form
        {
            private FileSystemWatcher _monitor;
    
            public Form1()
            {
                InitializeComponent();
                StartMonitor();
            }
    
            public void StartMonitor()
            {
                _monitor = new FileSystemWatcher();
                _monitor.InternalBufferSize = 16384;
                _monitor.Path = @"c:\DaleTest";
                _monitor.IncludeSubdirectories = false;
                _monitor.Filter = "DPJobState*.txt";
                _monitor.NotifyFilter = NotifyFilters.LastWrite;
                _monitor.Error += new ErrorEventHandler(OnMonitorError);
                _monitor.Changed += new FileSystemEventHandler(OnMonitorChanged);
                _monitor.EnableRaisingEvents = true;
            }
    
            private void OnMonitorChanged(object sender, FileSystemEventArgs e)
            {
                // turn off the raising of events
                _monitor.EnableRaisingEvents = false;
                _monitor.EnableRaisingEvents = true;
            }
    
            private void OnMonitorError(object source, ErrorEventArgs e)
            {
                Console.WriteLine("FileSystemWatcher error: ", e.GetException());
            }
        }
    }
    


    Dale Hoffman

    Friday, March 7, 2014 4:10 PM

Answers

  • To the best of my knowledge nothing changed in the FileSystemWatcher class itself. The change I know about was done in Windows, in the filesystem to be precise.

    Though since you say that the server is running Linux then it's probably something totally different. The fact that changing the filter makes it work is quite bizarre and it's more likely to be caused by who knows what issue with the file sharing protocol.

    Friday, March 14, 2014 5:21 PM
    Moderator

All replies

  • "written to by a machine to which the computer is attached"

    Does that mean that the log file is on a network drive? Was the computer which shares the drive upgraded to a newer version of Windows?

    Newer versions of Windows no longer raise file change events while the file is being modified, they raise a change event only when the file is closed. It's quite possible that the application which writes the log file never closes it so you'll never get a file change event.

    Saturday, March 8, 2014 7:57 AM
    Moderator
  • Yes, the computer that is running the FileSystemWatcher is monitoring a mapped drived to a Linux share on a computer sitting next to it.  The 'machine' writes to the log file on the Linux box and the FileSystemWatcher has been picking up the changes just fine.  It is entirely possible that the file is never closed and will explore that possibility.

    Also, over the weekend I discovered that if I modify the line

                _monitor.Filter = "DPJobState*.txt";

    to read

                _monitor.Filter = "DPJobState*.*";

    It once again works (at least in my debugging environment which is Win 8.0)


    Dale Hoffman

    Monday, March 10, 2014 3:15 PM
  • Still want to know what has changed with the FileSystemWatcher.  Why did it work for two years and then just suddenly stop?

    Dale Hoffman

    Friday, March 14, 2014 2:57 PM
  • To the best of my knowledge nothing changed in the FileSystemWatcher class itself. The change I know about was done in Windows, in the filesystem to be precise.

    Though since you say that the server is running Linux then it's probably something totally different. The fact that changing the filter makes it work is quite bizarre and it's more likely to be caused by who knows what issue with the file sharing protocol.

    Friday, March 14, 2014 5:21 PM
    Moderator