简体   繁体   English

如何删除超过7天C#/ NET的文件夹

[英]How to Delete folder older than 7 days C# / NET

I want to write some code that deletes all DIRECTORIES older than 7 days. 我想写一些代码来删除超过7天的所有DIRECTORIES。

So: 所以:

  1. Check directory : D:\\this 检查目录: D:\\this
  2. If folder older than 7 days -> delete it from the system. 如果文件夹超过7天 - >从系统中删除它。

You can lookup using the DirectoryInfo util 您可以使用DirectoryInfo util进行查找

   DirectoryInfo d = new DirectoryInfo(dir);
   if (d.CreationTime    < DateTime.Now.AddDays(-7))
       d.Delete();

You can use DirectoryInfo 您可以使用DirectoryInfo

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

The voice of experience says to include sanity checks in your code that the directories you are deleting are in fact ones that you want to be deleting... 经验之谈说,在您的代码中包含健全性检查,您要删除的目录实际上是您要删除的目录...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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