你可以检查一下缺少什么属性,特别是fw开始监视,需要开启相应的RaiseXXXX开关。

解决方案 »

  1.   

    fw.EnableRaisingEvents = true;
      

  2.   

    fw.EnableRaisingEvents = true;
      

  3.   

            [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') ;
            }
            private static void OnChanged(object source, FileSystemEventArgs e)
            {
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            }
            private static void OnRenamed(object source, RenamedEventArgs e)
            {
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
            }