简体   繁体   中英

How to poll directory for files

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM