FileSystemWatcher Changed event is raised twice

前端 未结 30 3839
死守一世寂寞
死守一世寂寞 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:13

    I spent some significant amount of time using the FileSystemWatcher, and some of the approaches here will not work. I really liked the disabling events approach, but unfortunately, it doesn't work if there is >1 file being dropped, second file will be missed most if not all times. So I use the following approach:

    private void EventCallback(object sender, FileSystemEventArgs e)
    {
        var fileName = e.FullPath;
    
        if (!File.Exists(fileName))
        {
            // We've dealt with the file, this is just supressing further events.
            return;
        }
    
        // File exists, so move it to a working directory. 
        File.Move(fileName, [working directory]);
    
        // Kick-off whatever processing is required.
    }
    

提交回复
热议问题