[英]How to poll directory for files
我想每5分鍾輪詢一次目錄中的文件。 我不想使用filesystemwatcher。 我是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();
將其放在計時器中可能是最好的解決方案,但是要使用哪個計時器類可能取決於此:您正在做控制台應用程序,Winforms還是WPF? 對於Winforms,請使用Timer。
對於wpf,請使用DispatcherTimer: 與C#計時器一樣的WPF計時器
為了回應您的明確評論,對於服務,請參見以下答案: 在Windows服務中使用的最佳計時器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.