Changed C # FileSystemWatcher class event triggered multiple times

 public  static  void the Main ( String [] args) 
        { 


            // create a new FileSystemWatcher and set its properties 
            FileSystemWatcher Watcher = new new FileSystemWatcher (); 
            watcher.Path = " F.: \\ " ;
             / * monitor changes LastAcceSS time and LastWrite and rename the file or directory * / 
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                   NotifyFilters.FileName | NotifyFilters.DirectoryName;
             // only monitors text file 
            watcher.Filter = " * .txt" ;
             // add an event handler
             // When a FileSystemWatcher the path specified file or directory
             // size, system attributes, last write time, last access time, or security permissions
             // when a change occurs, the change event occurs 
            + = watcher.Changed new new FileSystemEventHandler (the OnChanged);
             // when they are created by the FileSystemWatcher the path specified file or directory, create an event occurs
             // watcher.Created + = new new FileSystemEventHandler (the OnChanged);
             // when a FileSystemWatcher the specified path when a file or directory is deleted, delete an event occurs
             // watcher.Deleted + = new new FileSystemEventHandler (the OnChanged);
             // when a FileSystemWatcher the path specified file or directory was renamed, rename event occurs
             //+ = new new RenamedEventHandler watcher.Renamed (the OnRenamed);
             // start monitoring 
            watcher.EnableRaisingEvents = to true ;
             // wait for the user to exit the program
             // Console.WriteLine (the DateTime.Now + ", Press \ 'Q \' to quit The Sample." ); 
            the while (Console.Read () =! ' Q ' ); 
        } 
        // custom event handlers 
        public  static  void the OnChanged ( Object SENDER, the FileSystemEventArgs E) 
        { 
            var Watcher = SENDER AS the FileSystemWatcher; 
            watcher.EnableRaisingEvents =false ;
             // specify when a file is changed, created or deleted when things do 
            Console.WriteLine (DateTime.Now + " , File: " + e.FullPath + "" + e.ChangeType); 
            watcher.EnableRaisingEvents = to true ; 
        }

Solution:

Only at the start do first process: watcher.EnableRaisingEvents = false;

After the end did: watcher.EnableRaisingEvents = true;

Guess you like

Origin www.cnblogs.com/ChaunceyWan/p/11357789.html