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.

The post [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET appeared first on Software Development | Programming Tutorials.