In this post I will explain how you can delete files in a directory that are older than x days,months,year,minute or second.In this post, I will explain to you, how you can delete files in a certain directory older than N months , days or year.
I’m working on a project in which I need to delete the files that are older than 60 days, in other words, files created less than 60 days ago should be kept, all others should be deleted.
For achieving this task we can follow the below code. I have created a simple console application and written the simple c# code for you.
You can delete the files using the property of FileSystemInfo class.you can delete the file using CreationTime of file or LastModifiedDate of file.
If you want to delete files in a directory using LastModifiedDate of the file then you have to use LastWriteTime property of FileSystemInfo class.
You can get LastModifiedDate of the file by property LastWriteTime of FileSystemInfo class.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DeleteOlderFile(60);
}
public static void DeleteOlderFile(int numberdays)
{
try
{
//get all files from folder
string dirName = System.Web.HttpContext.Current.Server.MapPath("/DownloadedContent");
string[] files = Directory.GetFiles(dirName);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.CreationTime < DateTime.Now.AddMinutes(-numberdays))
fi.Delete();
}
}
catch (Exception ex)
{
}
}
}
}
You can also use 1 line Lamba Query:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DeleteOlderFileUsingLamda(60);
}
public static void DeleteOlderFileUsingLamda(int numberdays)
{
try
{
//get all files from folder
string dirName = System.Web.HttpContext.Current.Server.MapPath("/DownloadedContent");
Directory.GetFiles(dirName)
.Select(f => new FileInfo(f))
.Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-numberdays))
.ToList()
.ForEach(f => f.Delete());
}
catch (Exception ex)
{
}
}
}
}
Points In this Post:-
- Delete file from folder using last modified property c#
- Delete Old Files in a Folder in C#
- Removing files that are older than some number of days
The post [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- [Simple Way]-Add/Delete table rows dynamically using JavaScript
- How to edit/delete selected row from HTML table using JavaScript
- Inline table editing with edit and delete icon in React Js
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Implementing “Remember Me” Feature in ASP.NET MVC
- [Solved]-Cookie loses value when page is changed in MVC