【问题标题】:FileSystemWatcher not firing events when file written using fprintf or printf使用 fprintf 或 printf 写入文件时,FileSystemWatcher 不会触发事件
【发布时间】:2016-05-26 04:50:11
【问题描述】:

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

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

winform C# 应用程序中的 fileSystemWatcher 在编写器进程写入文件时收到通知,并且我以物理方式访问该文件,即 F5 文件夹或在文本板中打开它并单击打开的文件或右键单击该文件,但我从未得到任何当我不与文件进行物理交互时的事件通知。 此外,当我关闭编写器应用程序时,我确实会收到通知。

这是我的代码。

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;
}

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

    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))
            {

            }
        }
    }

【问题讨论】:

    标签: c# printf filesystemwatcher


    【解决方案1】:

    我敢打赌这个帖子有你的答案 --> FileSystemWatcher changed event (for “LastWrite”) is unreliable

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

    如果您手头有足够的时间和资源,那么您可能想研究File System FiltersFilter Manager and Minifilter Driver 的更简单方法。它是驱动程序类型的开发,但是,它是实现目标的可靠文件方式。

    它被系统策略挖掘得更深一些,但会给你一个wide array of events 来锁定。如果我为 pci 合规性或类似任务执行此操作,那么我不会使用 FileSystemWatcher。

    【讨论】:

    • 谢谢,猜测流打开/关闭引发事件。 Flush() 没有。所以在这种情况下我不能使用 fileysstemwatcher。我使用轮询。问题是文件在网络上,每隔几秒钟读取完整文件以获取增量是大量的网络负载。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多