FileSystemWatcher Changed event is raised twice

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

    FileReadTime = DateTime.Now;
    
    private void File_Changed(object sender, FileSystemEventArgs e)
    {            
        var lastWriteTime = File.GetLastWriteTime(e.FullPath);
        if (lastWriteTime.Subtract(FileReadTime).Ticks > 0)
        {
            // code
            FileReadTime = DateTime.Now;
        }
    }
    

提交回复
热议问题