delete files which are more than one month old using c#
-
30-09-2020 - |
Question
how to delete files which are more than one month old using c# script. i am using framework 2.0..
Solution
string path = @"C:\Temp\"; //"
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] fileInfos = dirInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
if (fileInfo.LastWriteTime < DateTime.Now.AddMonths(-1))
fileInfo.Delete();
}
OTHER TIPS
You can call Directory.GetFiles
to find all files in a folder.
You can call File.GetLastWriteTime
to check when the file was modified.
You can call File.Delete
to delete a file.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow