C# FileSystemWatcher 没有触发事件——为什么不呢?

C# FileSystemWatcher not firing off events - why not?

这是我的代码...在 Foo 目录中创建新文件时,不会触发任何 FileSystemWatcher 事件。我的代码有什么问题?

我正在通过调用 fileMonitorService.StartMonitoringForNewFiles(new RobotParameters()).

从 NUnit 测试中对此进行测试
// attribute for autofac IoC
    [AutoRegister]
    public class FileMonitorService
    {
        private FileSystemWatcher _fileWatcher = new FileSystemWatcher();

        public void StartMonitoringForNewFiles(RobotParameters parameters)
        {

        _fileWatcher.Path = @"c:\testfiles\foo";
            _fileWatcher.NotifyFilter = NotifyFilters.Attributes |
                NotifyFilters.CreationTime |
                NotifyFilters.FileName |
                NotifyFilters.LastAccess |
                NotifyFilters.LastWrite |
                NotifyFilters.Size |
                NotifyFilters.Security;

            _fileWatcher.Filter = "*.xlsx";
            _fileWatcher.Created += new FileSystemEventHandler((sender, e)  => OnFileCreated(sender, e, parameters));
            _fileWatcher.Renamed += new RenamedEventHandler((sender, e) => OnFileRenamed(sender, e, parameters));
            _fileWatcher.Error += new ErrorEventHandler(OnError);
            _fileWatcher.EnableRaisingEvents = true;
            }

        public void StopMonitoringForFiles()
        {
_fileWatcher.EnableRaisingEvents = false;
        }

        private void OnFileRenamed(object sender, RenamedEventArgs e, RobotParameters parameters)
        {
            parameters.FileToProcess = e.FullPath;
            parameters.FileCreated = true;
        }

        private void OnFileCreated(object sender, FileSystemEventArgs e, RobotParameters parameters)
        {
            parameters.FileToProcess = e.FullPath;
            parameters.FileCreated = true;
        }

        private void OnError(object sender, ErrorEventArgs e)
        {
            var foo = e.GetException();
        }


    }

我一直无法让它工作,根据我的 google 研究,许多其他人也遇到过同样的问题。我也尝试使用 System.Timers.Timer 并且它的 Elapsed 事件也不会引发。 Google 研究表明很多其他人也有这个问题。我所有的代码都在 try/catch 中,并且两者都没有引发任何错误。不幸的是,我不得不编写自己的文件监视器代码,虽然它不漂亮,但它可以工作。我将它发布给可能遇到此问题并且可能正在寻找自定义解决方案的任何其他人。

public  class FileMonitorService
    {
        private bool stopProcessing = false;

        public  void StartMonitoringForNewFiles(RobotParameters parameters)
        {
            try
            {
                while (stopProcessing == false)
                {

                        stopProcessing = IsFileReady(parameters);
                    Thread.Sleep(5000);      // sleeps for 5 seconds 

                    if (stopProcessing == true)
                    {
                        break;      // get out of loop since file is ready
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }

        private bool IsFileReady(RobotParameters parameters)
        {
                try
                {
// code to try opening a file goes here
// or code to see if files exist goes here

// assuming file was opened with no errors and is ready for processing

                    return true;
                }
                catch (Exception e)
                {

// file is not ready for processing
// and could not be opened
                    return false;

                }
            }


            return false;

            }


    }

得到答案永远不会太迟。您只需稍作调整:

_fileWatcher.Created += new FileSystemEventHandler((sender, e) => OnFileCreated(sender, e, parameters));
_fileWatcher.Renamed += new RenamedEventHandler((sender, e) => OnFileRenamed(sender, e, parameters));

只需删除新的 FileSystemEventHandler:

_fileWatcher.Created += (sender, e) => OnFileCreated(sender, e, parameters);
_fileWatcher.Renamed += (sender, e) => OnFileRenamed(sender, e, parameters);

这对我有用。