使用fprintf或printf写入文件时,FileSystemWatcher不触发事件 [英] FileSystemWatcher not firing events when file written using fprintf or printf

查看:114
本文介绍了使用fprintf或printf写入文件时,FileSystemWatcher不触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,有很多类似的问题,但是没有一个能解决我的独特情况.

Yes there are many similar questions but none of them address my unique situation.

有一个单独的c ++进程使用c ++ printf和fprintf写入文件. 我要观看的文件名是info_20160525.log

There is a separate c++ process writing the file using c++ printf and fprintf. filename i am trying to watch is info_20160525.log

当编写器进程将文件写入文件时,我在winform C#应用程序中的fileSystemWatcher会收到通知,并且我实际访问该文件(即F5文件夹)或在文本板上打开它,然后单击打开的文件或右键单击该文件,但是我从没收到任何文件当我不与文件进行物理交互时发生事件通知. 另外,当我关闭编写器应用程序时,也会收到通知.

My fileSystemWatcher in winform C# application gets notification when the writer process writes to the file AND I physically access the file i-e F5 the folder or have it open in textpad and click the opened file or right click the file but I never get any event notification when I dont physically interact with the file. Also, when I shutdown the writer application I do get the notification.

这是我的代码.

public bool StartUp(string fullfilepath, int linenumber)
{
        if (!File.Exists(fullfilepath))
            return false;
        if (!LogClass.CheckPathExists(m_applicationPath)) 
            return false;

        try
        {

            FileInfo info = new FileInfo(fullfilepath);

            m_filename = fullfilepath;
            m_fileWatcher = new FileSystemWatcher(info.DirectoryName, info.Name);
            m_fileWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.LastAccess
       | NotifyFilters.LastWrite | NotifyFilters.Size ;    

            m_fileWatcher.Changed += m_fileWatcher_Changed;
            m_fileWatcher.Error += m_fileWatcher_Error;
            m_fileWatcher.Created += m_fileWatcher_Created;
            m_fileWatcher.Deleted += m_fileWatcher_Deleted;
            m_fileWatcher.Disposed += m_fileWatcher_Disposed;
            m_fileWatcher.Renamed += m_fileWatcher_Renamed;
            m_linesRead = linenumber;
            m_fileWatcher.EnableRaisingEvents = true;
        }
        catch(Exception e)
        {
            LogClass.LogError(e, "Trouble accessing the file" + fullfilepath, m_applicationPath);
        }
        return true;
}

这些是处理程序.我每个人都有断点,但是除非我与文件进行物理交互,否则我永远也不会触发.

These are the handlers. I have breakpoints in each one of them but I never get a trigger unless ofcourse I physically interact with the file.

    void m_fileWatcher_Renamed(object sender, RenamedEventArgs e)
    {
        string S = "";
    }

    void m_fileWatcher_Disposed(object sender, EventArgs e)
    {
        string S = "";
    }

    void m_fileWatcher_Deleted(object sender, FileSystemEventArgs e)
    {
        string S = "";
    }

    void m_fileWatcher_Created(object sender, FileSystemEventArgs e)
    {
        string S = "";
    }

    void m_fileWatcher_Error(object sender, ErrorEventArgs e)
    {
        string S = "";

    }

    void m_fileWatcher_Changed(object sender, FileSystemEventArgs args)
    {
        if (args.ChangeType == WatcherChangeTypes.Changed)
        {                
            while (ParseFile(args.FullPath))
            {

            }
        }
    }

推荐答案

我敢打赌这个线程有您的答案->

I bet this thread has your answer --> FileSystemWatcher changed event (for "LastWrite") is unreliable

FileSystemWatcher使用对文件的LastWrite属性的更新来引发事件,但是,LastWrite不会实时更新,因此不应将其作为事件的触发器.

The FileSystemWatcher uses an update to the LastWrite attribute of a file to fire events, however, the LastWrite is not updated in real time and should not be relied upon as trigger for an event.

如果您有足够的时间和资源,那么您可能想研究过滤器管理器和微型过滤器驱动程序.它是驱动程序类型的开发,但是,这是完成目标的可靠文件方式.

If you have enough time and resources on your hands then you probably want to look into File System Filters and the simpler approach of a Filter Manager and Minifilter Driver. It is driver type development, however, it is a sure file way to accomplish your objective.

通过系统策略对其进行了更深入的研究,但为您提供了

It is dug down a little deeper by system policy but gives you a wide array of events to latch onto. If I was doing this for anything like pci compliance or similar tasks then I would not use the FileSystemWatcher.

这篇关于使用fprintf或printf写入文件时,FileSystemWatcher不触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆