2

I want to poll a directory for files every 5 minutes. I do NOT want to use filesystemwatcher. I am fairly new to c# and I cant find any good examples

This is what I have so far. Do I just put this in a timer?

    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();
user2892443
  • 103
  • 1
  • 2
  • 5
  • 2
    _Why_ don't you want to use FSW? That's like asking `How do I slice fruit? I do NOT want to use a knife`. – SLaks Oct 21 '13 at 21:34
  • Im noticing signifigant file locks and I have found that it is missing files. – user2892443 Oct 21 '13 at 21:40
  • When you get the event from FileSystemWatcher, you can actually have several queued at once. I process the entire directory I'm watching so that I pick up all files that are present. As to file locks that is a timing issue that will need to be addressed regardless of how you do this. – SASS_Shooter Oct 21 '13 at 21:54
  • So how would I need to change the timing if I were to keep using fsw? – user2892443 Oct 21 '13 at 22:00
  • 3
    FileSystemWatcher is trouble. Please, don't use it for no reason. http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes – programad May 07 '14 at 18:03
  • @SLaks - The reason I'm on this page is that FSW gives completely different performance on different computers. I used it to monitor updates to log files - worked fine on two Windows 7 test machines, moved it across to another Windows 7 machine and the same code broke - no events were raised at all. A quick search of these forums, and others, shows that lots of people have had similar issues with FSW being unreliable. – Orphid Jan 15 '15 at 16:22

1 Answers1

1

Putting it in a timer is probably your best solution, but which timer class to use may depend on this: Are you doing a console app, Winforms, or WPF? For Winforms use Timer.

For wpf use DispatcherTimer: WPF Timer Like C# Timer

In response to your clarifying comment, for services see this answer: Best Timer for using in a Windows service

Community
  • 1
  • 1
philologon
  • 1,981
  • 3
  • 17
  • 30
  • It is part of a service that processes the file and moves it from one folder to another – user2892443 Oct 21 '13 at 21:39
  • Answering questions on SO helps me learn as much as asking them: http://stackoverflow.com/questions/246697/windows-service-and-timer Note that I found this by searching SO for [C#] timer service. – philologon Oct 21 '13 at 21:42