文件更改/创建时,FileSystemWatcher无法处理条件

时间:2012-07-10 07:14:14

标签: c# filesystemwatcher

我使用FileSystemWatcher监视文件将生成的文件夹。我遇到的问题是可以生成两个不同的文件,因此我在onChanged事件中创建了一个if语句来处理它。代码如下所示:

    public void OnFileChanged(object sender, FileSystemEventArgs e)
    {

        var fileName = Path.GetFileNameWithoutExtension(e.Name);

        if (string.IsNullOrEmpty(fileName))
        {
            //Console.WriteLine("Could not get file name..");
            return;
        }

        if (fileName.EndsWith(".Error"))
        {
            if (File.Exists(e.FullPath))
            {
                lock (QueueManager.FooLocker)
                {
                    // does the file already exists in the queue?
                    if (_manager.FooQueue.Any(a => a == e.FullPath))
                    {
                        // File has already been tagged for processing later..
                        return;
                    }
                    // ... otherwise, just add to to the queue to be processed later on..
                    _manager.FooQueue.Enqueue(e.FullPath);
                }
            }
        }

        else
        {
            if (File.Exists(e.FullPath))
            {
                lock (QueueManager.BarLocker)
                {
                    // does the file already exists in the queue?
                    if (_manager.BarQueue.Any(a => a == e.FullPath))
                    {
                        // File has already been tagged for processing later..
                        return;
                    }
                    // ... otherwise, just add to to the queue to be processed later on..
                    _manager.BarQueue.Enqueue(e.FullPath);
                }
            }
        }
    }

在Created-event中,我有一个类似的if条件,它可以正常工作。但是在Changed-event中它只会运行" else"部分,因为该文件不断变化。如何让Watcher交替处理这些变化?

0 个答案:

没有答案