日期:2014-05-16  浏览次数:20942 次

做了一个FileSystemWatcher的实验,发现没有起到作用,求教!
我发现程序起来以后,等待键盘输入,此时,进入我的电脑,在d:\下面创建一个文件夹。程序并没有什么反应啊,什么时候才会输出"检测到变化"呢?

            try
            {
                var fw = new FileSystemWatcher(@"d:\");
                fw.Changed += (source, e) => Console.WriteLine("检测到变化");
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

我期待这个程序启动以后,无论何时d:\发生了变化,程序都能检测到。我的理解哪里出现了问题呢,程序要怎么改?
------解决方案--------------------
fw.EnableRaisingEvents = true;

------解决方案--------------------
fw.EnableRaisingEvents = true;
------解决方案--------------------


        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        static void Main(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.NotifyFilter = NotifyFilters.LastAccess 
------解决方案--------------------
 NotifyFilters.LastWrite
               
------解决方案--------------------
 NotifyFilters.FileName 
------解决方案--------------------
 NotifyFilters.DirectoryName;
            watcher.Filter = "*.*";
            watcher.Path = "D:\\test";
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            watcher.EnableRaisingEvents = true;
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }
      &n