how to delete files which are more than one month old using c# script. i am using framework 2.0..
Asked
Active
Viewed 1,100 times
2
-
Can you give us some more info, delte them? what for? You want to scan your entire pc for files over 1 month old and delete them all ? – Nealv Jul 27 '10 at 13:26
2 Answers
8
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();
}

Joel Coehoorn
- 399,467
- 113
- 570
- 794

Anthony Pegram
- 123,721
- 27
- 225
- 246
3
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.

SLaks
- 868,454
- 176
- 1,908
- 1,964