FileSystemWatcher Changed event is raised twice

前端 未结 30 3269
死守一世寂寞
死守一世寂寞 2020-11-22 06:01

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event

30条回答
  •  粉色の甜心
    2020-11-22 06:36

    Here is a new solution you can try. Works well for me. In the event handler for the changed event programmatically remove the handler from the designer output a message if desired then programmatically add the handler back. example:

    public void fileSystemWatcher1_Changed( object sender, System.IO.FileSystemEventArgs e )
        {            
            fileSystemWatcher1.Changed -= new System.IO.FileSystemEventHandler( fileSystemWatcher1_Changed );
            MessageBox.Show( "File has been uploaded to destination", "Success!" );
            fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler( fileSystemWatcher1_Changed );
        }
    

提交回复
热议问题