本文介绍了.NET FileSystemWatcher的 - 这是一个文件或目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来确定与FSW如果一个文件或目录被删除了?

Is there a way to determine with the FSW if a file or a directory has been deleted?

推荐答案

以下是弗莱彻的解决方案的简化和修正版本:

Here's a simplified and corrected version of fletcher's solution:

namespace Watcher
{
    class Program
    {
        private const string Directory = @"C:\Temp";
        private static FileSystemWatcher _fileWatcher;
        private static FileSystemWatcher _dirWatcher;

        static void Main(string[] args)
        {
             _fileWatcher = new FileSystemWatcher(Directory);
             _fileWatcher.IncludeSubdirectories = true;
             _fileWatcher.NotifyFilter = NotifyFilters.FileName;
             _fileWatcher.EnableRaisingEvents = true;
             _fileWatcher.Deleted += WatcherActivity;

            _dirWatcher = new FileSystemWatcher(Directory);
            _dirWatcher.IncludeSubdirectories = true;
            _dirWatcher.NotifyFilter = NotifyFilters.DirectoryName;
            _dirWatcher.EnableRaisingEvents = true;
            _dirWatcher.Deleted += WatcherActivity;

            Console.ReadLine();
        }

        static void WatcherActivity(object sender, FileSystemEventArgs e)
        {
            if(sender == _dirWatcher)
            {
                Console.WriteLine("Directory:{0}",e.FullPath);
            }
            else
            {
                Console.WriteLine("File:{0}",e.FullPath);
            }
        }
    }
}

这篇关于.NET FileSystemWatcher的 - 这是一个文件或目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:35