FileSystemWatcher Changed event is raised twice

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

    Try this!

    string temp="";
    
    public void Initialize()
    {
       FileSystemWatcher _fileWatcher = new FileSystemWatcher();
      _fileWatcher.Path = "C:\\Folder";
      _fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
      _fileWatcher.Filter = "Version.txt";
      _fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
      _fileWatcher.EnableRaisingEvents = true;
    }
    
    private void OnChanged(object source, FileSystemEventArgs e)
    {
       .......
    if(temp=="")
    {
       //do thing you want.
       temp = e.name //name of text file.
    }else if(temp !="" && temp != e.name)
    {
       //do thing you want.
       temp = e.name //name of text file.
    }else
    {
      //second fire ignored.
    }
    
    }
    

提交回复
热议问题