[Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET

[Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET

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
When I was working on a C# Windows program where I need to check the creation time of a file and need to delete it if the file is older than 7 days.
My Windows app is running constantly and generates tons of files and a separate thread checks that the files are no older than say 7 days. For that, I have written above code to remove files older than say 7 days and it’s working fine.
If you have any queries or doubt please comment.

Ashok Patel

I'm an electronic engineer working in a multinational company,having good experience on Electronics and electrical engineers design and oversee production of electronic equipment such as radios, televisions, computers, washing machines and telecommunication systems.I like to do RND and Research.I also have hands on experience graphic design software and in web designing having great command on ASP.NET, HTML5, JavaScript, T-SQL, JQuery.

Add comment

Donate for Corona Victim

Corona Virus Relief Fund

Your Header Sidebar area is currently empty. Hurry up and add some widgets.