从4天以上的文件夹中删除文件 [英] Delete files from the folder older than 4 days
本文介绍了从4天以上的文件夹中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想每5个小时运行一个计时器,并从4天以上的文件夹中删除文件。可以用示例代码吗?
I would like to run a timer for every 5 hours and delete the files from the folder older than 4 days. Could you please with sample code?
推荐答案
由于尚未提及,我建议使用 System.Threading.Timer
这样的东西。这是一个示例实现:
Since it hasn't been mentioned, I would recommend using a System.Threading.Timer
for something like this. Here's an example implementation:
System.Threading.Timer DeleteFileTimer = null;
private void CreateStartTimer()
{
TimeSpan InitialInterval = new TimeSpan(0,0,5);
TimeSpan RegularInterval = new TimeSpan(5,0,0);
DeleteFileTimer = new System.Threading.Timer(QueryDeleteFiles, null,
InitialInterval, RegularInterval);
}
private void QueryDeleteFiles(object state)
{
//Delete Files Here... (Fires Every Five Hours).
//Warning: Don't update any UI elements from here without Invoke()ing
System.Diagnostics.Debug.WriteLine("Deleting Files...");
}
private void StopDestroyTimer()
{
DeleteFileTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
DeleteFileTimer.Dispose();
}
这样,您可以在Windows服务中运行文件删除代码麻烦
This way, you can run your file deletion code in a windows service with minimal hassle.
这篇关于从4天以上的文件夹中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文