日期:2014-05-19  浏览次数:20737 次

FileSystemWatcher问题,大家帮忙看看
using   System;
using   System.Collections.Generic;
using   System.Text;
using   System.IO;

namespace   WatchApplication
{
        class   MyWatch
        {
                private   FileSystemWatcher   watcher;
                string   path;
                public   MyWatch(string   path)
                {
                        watcher   =   new   FileSystemWatcher();
                        this.path   =   path;
                        this.InitializeWatch();
                }

                private   void   InitializeWatch()
                {
                        this.watcher.Path   =   this.path;
                        this.watcher.NotifyFilter   =   NotifyFilters.LastAccess   |   NotifyFilters.LastWrite
                              |   NotifyFilters.FileName   |   NotifyFilters.DirectoryName;
                        this.watcher.Filter   =   "*.txt ";
                        this.watcher.Changed   +=   new   FileSystemEventHandler(this.OnChanged);
                        this.watcher.Created   +=   new   FileSystemEventHandler(this.OnChanged);
                        this.watcher.Deleted   +=   new   FileSystemEventHandler(this.OnChanged);
                        this.watcher.Renamed   +=   new   RenamedEventHandler(this.OnRenamed);
                }

                public   void   StartWatch()
                {
                        this.watcher.EnableRaisingEvents   =   true;
                }

                private   void   OnChanged(object   source,   FileSystemEventArgs   e)
                {
                        Console.WriteLine( "File:   "   +   e.FullPath   +   "   "   +   e.ChangeType);
                }