Following are the C# code to delete files from folder those are older than some number of days.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _02_DeleteOldFilesFromFolder
{
class Program
{
static void Main(string[] args)
{
Declare Variable and Set Values
string FileDirectory;
int RetentionPeriodinDays;
FileDirectory = CSource;
RetentionPeriodinDays = 2;
var files = new DirectoryInfo(FileDirectory).GetFiles(.);
foreach (var file in files)
{
if (file.CreationTime DateTime.Today.AddDays(-RetentionPeriodinDays))
{
file.Delete();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _02_DeleteOldFilesFromFolder
{
class Program
{
static void Main(string[] args)
{
Declare Variable and Set Values
string FileDirectory;
int RetentionPeriodinDays;
FileDirectory = CSource;
RetentionPeriodinDays = 2;
var files = new DirectoryInfo(FileDirectory).GetFiles(.);
foreach (var file in files)
{
if (file.CreationTime DateTime.Today.AddDays(-RetentionPeriodinDays))
{
file.Delete();
}
}
}
}
}