【问题标题】:How to poll directory for files如何轮询文件目录
【发布时间】:2013-10-30 13:44:53
【问题描述】:

我想每 5 分钟轮询一次目录中的文件。我不想使用文件系统观察器。我对 c# 还很陌生,我找不到任何好的例子

这是我目前所拥有的。我只是把它放在一个计时器里吗?

    string watchFolder = ConfigurationManager.AppSettings["watchFolder"];
    DirectoryInfo directoryInfo = new DirectoryInfo(watchFolder);


    if (!Directory.Exists(watchFolder))
    {
        Console.WriteLine(
            "{0} directory does not exist. Please modify the config file accordingly.",
            watchFolder);
        Environment.Exit(3);
    }

    FileInfo[] lastUpdatedFiles = directoryInfo.GetFiles();

【问题讨论】:

  • 为什么你不想使用 FSW?这就像问How do I slice fruit? I do NOT want to use a knife
  • 我注意到重要的文件锁,我发现它丢失了文件。
  • 当您从 FileSystemWatcher 获取事件时,您实际上可以同时有多个排队。我处理我正在观看的整个目录,以便获取所有存在的文件。至于文件锁定,这是一个时间问题,无论您如何执行此操作,都需要解决。
  • 如果我要继续使用 fsw,我需要如何更改时间?
  • FileSystemWatcher 有问题。请不要无缘无故地使用它。 stackoverflow.com/questions/239988/…

标签: c# timer polling


【解决方案1】:

将它放入计时器可能是您最好的解决方案,但使用哪个计时器类可能取决于:您是在做控制台应用程序、Winforms 还是 WPF?对于 Winforms,请使用 Timer。

对于 wpf 使用 DispatcherTimer:WPF Timer Like C# Timer

针对您的澄清评论,有关服务,请参阅以下答案: Best Timer for using in a Windows service

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-02-24
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 2011-03-01
  • 2015-10-28
相关资源
最近更新 更多