2

I'm trying to create a simple app, what moves all files writed to some directory to other directory. That's my problem: if i write other than 10000 files at once in my directory(small .txt files over 1KB) - some of them not handling to move on output directory. I'm using FileSystemWatcher events handler to solve this problem. Here is my code example:

Class MyProgramm
{
    void Process(Object o, FileSystemEventArgs e)
    {
        //do something with e.Name file
    }
    void main()
    {
        var FSW = New FileSystemWatcher{Path = "C:\\InputDir"};
        FSW.Created += Process;
        FSW.EnableRisingEvents = true;
        Thread.Sleep(Timeout.Infinite);
    }
}

Finally, we got some files processed, but some of written files stays unprocessed.. Any suggestions?

user1648361
  • 23
  • 1
  • 3
  • possible duplicate of [FileSystemWatcher vs polling to watch for file changes](http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes) – Tony Hopkinson Sep 05 '12 at 09:03
  • please clarify your problem. Is this a problem where FileSystemWatcher fails to detect a moved file? Or is it that the files themselves fail to move from input to output directory? – Simon Ejsing Sep 05 '12 at 09:06
  • Have a look at a related post http://stackoverflow.com/questions/1286114/detecting-moved-files-using-filesystemwatcher – Conrad Lotz Sep 05 '12 at 09:10

1 Answers1

0

I had similar issues with the FileSystemWatcher. It seemed to "drop the ball" quite regularly. I went for an alternative solution by extending "ServiceBase", which has been working consistently since it went live as a windows service. Hope this helps:

    public partial class FileMonitor : ServiceBase
    {
        private Timer timer;
        private long interval;
        private const string ACCEPTED_FILE_TYPE = "*.csv";

        public FileMonitor()
        {           
            this.ServiceName = "Service";
            this.CanStop = true;
            this.CanPauseAndContinue = true;
            this.AutoLog = true;
            this.interval = long.Parse(1000);
        }

        public static void Main()
        {
            ServiceBase.Run(new FileMonitor());
        }

        protected override void OnStop()
        {
            base.OnStop();
            this.timer.Dispose();
        }

        protected override void OnStart(string[] args)
        {
            AutoResetEvent autoEvent = new AutoResetEvent(false);
            this.timer = new Timer(new TimerCallback(ProcessNewFiles), autoEvent, interval, Timeout.Infinite);
        }

        private void ProcessNewFiles(Object stateInfo)
        {
            DateTime start = DateTime.Now;
            DateTime complete = DateTime.Now;

            try
            {
                string directoryToMonitor = "c:\mydirectory";
                DirectoryInfo feedDir = new DirectoryInfo(directoryToMonitor);
                FileInfo[] feeds = feedDir.GetFiles(ACCEPTED_FILE_TYPE, SearchOption.TopDirectoryOnly);             

                foreach (FileInfo feed in feeds.OrderBy(m => m.LastWriteTime))
                {
                    // Do whatever you want to do with the file here
                }
            }
            finally
            {
                TimeSpan t = complete.Subtract(start);
                long calculatedInterval = interval - t.Milliseconds < 0 ? 0 : interval - t.Milliseconds;
                this.timer.Change(calculatedInterval, Timeout.Infinite);
            }
        }       
    }
MiiisterJim
  • 419
  • 1
  • 4
  • 16
  • 3
    Thanks for your answers! The problem were in InternalBufferSize property of FileSystemWatcher! In default it equals 8192 bytes while each event gets 16 bytes from it. I just increased value of this property! Thanks! – user1648361 Sep 05 '12 at 10:54