[英]Check with a timer every second, if a file exist in a given path
I have a simple console application made with C#, in which I create a zip file. 我有一个用C#编写的简单控制台应用程序,我在其中创建了一个zip文件。 From another console application, I let a timer tick every second to check if the directory where I create the .zip file in the first app contains a .zip file, but it doesn't work.
从另一个控制台应用程序,我让计时器每秒打勾,以检查我在第一个应用程序中创建.zip文件的目录是否包含.zip文件,但它不起作用。
I implemented the code: 我实现了代码:
public class Program
{
private static System.Timers.Timer aTimer;
private static String path = "C:\\Path\\*.zip";
public static void Main()
{
SetTimer();
Console.WriteLine("\nChecking the ZIP file is created on the directory...\n");
Console.WriteLine("The application started at {0:HH:mm:ss}", DateTime.Now);
Console.ReadLine();
}
private static void SetTimer()
{
if (!File.Exists(path))
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(1000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
else
{
Console.WriteLine("ZIP file created.");
aTimer.Stop();
aTimer.Dispose();
Console.WriteLine("Terminating the application...");
}
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss}",
e.SignalTime);
}
}
Anyone could help me? 有人可以帮帮我吗? Thanks!
谢谢!
File.Exists
does not support generic wildcard like *
File.Exists
不支持像*
这样的通用通配符
Even, FileSystemWatcher is a better way to do this kind of task, here is what you can use immediatly to solve your issue 甚至, FileSystemWatcher是执行此类任务的更好方法,您可以立即使用它来解决您的问题
if (!new DirectoryInfo("c:\\path").EnumerateFiles("*.zip").Any())
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(1000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.