使用.NET在目录中删除3个月以前的文件

我想知道(使用C#)如何删除超过3个月的特定目录中的文件,但我想date可以灵活。

只是要清楚:我正在查找超过90天的文件,换句话说,应该保留90天以前创build的文件,删除所有其他文件。

像这样outta做这件事。

using System.IO; string[] files = Directory.GetFiles(dirName); foreach (string file in files) { FileInfo fi = new FileInfo(file); if (fi.LastAccessTime < DateTime.Now.AddMonths(-3)) fi.Delete(); } 

这是一个1class的lambda:

 Directory.GetFiles(dirName) .Select(f => new FileInfo(f)) .Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-3)) .ToList() .ForEach(f => f.Delete()); 

对于那些喜欢过度使用LINQ。

 (from f in new DirectoryInfo("C:/Temp").GetFiles() where f.CreationTime < DateTime.Now.Subtract(TimeSpan.FromDays(90)) select f ).ToList() .ForEach(f => f.Delete()); 

下面是如何获取目录中文件的创build时间的一个片段,并find3个月前(90天前确切)创build的文件:

  DirectoryInfo source = new DirectoryInfo(sourceDirectoryPath); // Get info of each file into the directory foreach (FileInfo fi in source.GetFiles()) { var creationTime = fi.CreationTime; if(creationTime < (DateTime.Now- new TimeSpan(90, 0, 0, 0))) { fi.Delete(); } } 

System.IO.File类的GetLastAccessTime属性应该有所帮助。

基本上你可以使用Directory.Getfiles(Path)来获取所有文件的列表。 之后,循环访问列表,并像Keithbuild议的那样调用GetLastAccessTim()。

你只需要FileInfo – > CreationTime

而不仅仅是计算时差。

在app.config中,您可以保存文件必须被删除的时间的TimeSpan值

也检查出DateTime Subtract方法。

祝你好运

或者,如果您需要根据创builddate删除文件,则可以使用File.GetCreationTime方法 。

就是这样

  foreach (FileInfo file in new DirectoryInfo("SomeFolder").GetFiles().Where(p => p.CreationTime < DateTime.Now.AddDays(-90)).ToArray()) File.Delete(file.FullName); 
  system.IO; List<string> DeletePath = new List<string>(); DirectoryInfo info = new DirectoryInfo(Server.MapPath("~\\TempVideos")); FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray(); foreach (FileInfo file in files) { DateTime CreationTime = file.CreationTime; double days = (DateTime.Now - CreationTime).TotalDays; if (days > 7) { string delFullPath = file.DirectoryName + "\\" + file.Name; DeletePath.Add(delFullPath); } } foreach (var f in DeletePath) { if (File.Exists(F)) { File.Delete(F); } } 

在页面加载或webservice或任何其他用途使用。

我的概念是evrry 7天,我必须删除文件夹文件,而不使用数据库

  //Store the number of days after which you want to delete the logs. int Days = 30; // Storing the path of the directory where the logs are stored. String DirPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6) + "\\Log(s)\\"; //Fetching all the folders. String[] objSubDirectory = Directory.GetDirectories(DirPath); //For each folder fetching all the files and matching with date given foreach (String subdir in objSubDirectory) { //Getting the path of the folder String strpath = Path.GetFullPath(subdir); //Fetching all the files from the folder. String[] strFiles = Directory.GetFiles(strpath); foreach (string files in strFiles) { //For each file checking the creation date with the current date. FileInfo objFile = new FileInfo(files); if (objFile.CreationTime <= DateTime.Now.AddDays(-Days)) { //Delete the file. objFile.Delete(); } } //If folder contains no file then delete the folder also. if (Directory.GetFiles(strpath).Length == 0) { DirectoryInfo objSubDir = new DirectoryInfo(subdir); //Delete the folder. objSubDir.Delete(); } } 

我试试这个代码,它工作得很好,希望这个答案

 namespace EraseJunkFiles { class Program { static void Main(string[] args) { DirectoryInfo yourRootDir = new DirectoryInfo(@"C:\yourdirectory\"); foreach (FileInfo file in yourRootDir.GetFiles()) if (file.LastWriteTime < DateTime.Now.AddDays(-90)) file.Delete(); } } } 
 try { FileInfo fi = new FileInfo(crop_name); if (fi.LastAccessTime < DateTime.Now.AddSeconds(-60)) { fi.Delete(); } } catch(Exception) { } 

Private Sub DeleteZip()

  Dim eachFileInMydirectory As New DirectoryInfo("D:\Test\") Dim fileName As IO.FileInfo Try For Each fileName In eachFileInMydirectory.GetFiles If fileName.Extension.Equals("*.zip") AndAlso (Now - fileName.CreationTime).Days > 90 Then fileName.Delete() End If Next Catch ex As Exception WriteToLogFile("No Files older than 90 days exists be deleted " & ex.Message) End Try End Sub