Filesystemwatcher double entries

假装没事ソ 提交于 2019-12-04 17:44:06

You need to keep track of files (in a collection or dictionary) that already raised an event by the FileSystemWatcher. According to MSDN

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

public void OnChanged(object source, FileSystemEventArgs e)
{
    try
    {
        watcher.EnableRaisingEvents = false;
        FileInfo objFileInfo = new FileInfo(e.FullPath);
        if (!objFileInfo.Exists) return;
        System.Threading.Thread.Sleep(5000);

        FileInfo fileinformatie = new FileInfo(e.FullPath);
        string strCreateTime = fileinformatie.CreationTime.ToString();
        string strCreateDate = fileinformatie.CreationTime.ToString();

        //Ignore this, only for my file information.
        strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
        strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));

        ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
    }
    catch (Exception ex)
    {
        ToolLabel.Text = ex.Message.ToString();
    }
    finally
    {
        watcher.EnableRaisingEvents = true;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!