【问题标题】:Monitoring a folder for new files in Windows在 Windows 中监视文件夹中的新文件
【发布时间】:2008-10-28 21:29:55
【问题描述】:

监视文件夹以查看何时添加了图像文件的最佳方法是什么?文件大约每分钟添加一次,命名如下... image0001.jpg、image0002.jpg、image0003.jpg 等。我需要知道文件何时写入文件夹,以便我的应用程序可以访问和使用它。

【问题讨论】:

    标签: c++ visual-c++


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      如前所述,目录更改通知是您想要的。

      我也研究过它们,我看到的警告是,当文件开始写入文件夹时,Windows 会触发通知。如果文件足够大,那么您将在文件完成写入之前收到通知。

      查看this google search 了解等待文件完全写入的各种解决方案

      编辑:我刚刚看到这个问题用 c++ 标记,我链接到一个 .Net 搜索。尽管我提供的语言可能不是正确的,但我认为无论您使用什么系统进行编码,您在 Windows 上仍然会遇到同样的问题。

      【讨论】:

        【解决方案3】:

        FileSystemWatcher 应该可以为您做到这一点。

        【讨论】:

          【解决方案4】:

          更改通知可能会导致一些开销,如果您使用的是 NTFS,请考虑NTFS change journals

          【讨论】:

            【解决方案5】:

            您可以使用轮询方法来监控文件夹。例如,循环将每 5 秒执行一次。

            此方法返回一个新文件列表:

            List<string> files = new List<string>();
            string path = @"C:\test\"; // whatever the path is
            
            public List<string> GetNewFiles(string path)
                {
                    // store all the filenames (only .jpg files) in a list
                    List<string> currentFiles = System.IO.Directory.GetFiles(path, "*.jpg");
            
                    if ( currentFiles.Count() > files.Count() )
                    {
                        count = newFiles.Length - files.Length;
                        List<string> newFiles = new List<string>();
            
                        foreach ( string file in currentFiles )
                        {
                            if ( !files.Contains(file) )
                            {
                                newFiles.Add(file);
                            }
                         }
                     }
                     files = currentFiles;
                     return newFiles;
                }
            

            这是每 5 秒轮询一次并调用前一个方法的方法。

            public void MonitorFolder()
            {
                while (true)
                {
                    List<string> newFiles = GetNewFiles(path);
                    System.Threading.Thread.Sleep(5000); // 5000 milliseconds
                }
            }
            

            【讨论】:

              【解决方案6】:
              1. Synch.variant FindFirstChangeNotification
              2. Asynch.variant ReadDirectoryChangesW

              【讨论】:

                【解决方案7】:

                这是我的搜索结果中排名靠前的 google 搜索结果,因此我将其添加为答案。

                如果您使用的是 Qt,则有 QFileSystemWatcher。我不知道这存在,而且我们碰巧在使用 Qt,所以我浪费了几个多小时使用 FindFirstChangeNotification 重写了我随时可用的内容,直到一位同事向我展示了这一点。

                不过学习体验不错。

                【讨论】:

                  【解决方案8】:

                  inotify 可能是你的菜

                  【讨论】:

                  • 不幸的是,Inotify 仅适用于 Linux。
                  • 嗯。我的印象是这些库被移植到不同的平台。
                  猜你喜欢
                  • 2021-08-08
                  • 2019-03-09
                  • 2013-08-18
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-10-05
                  • 2022-01-01
                  相关资源
                  最近更新 更多