tags:

views:

89

answers:

2

hello folks., how to delete files which are more than one month old using c# script. i am using framework 2.0..

+2  A: 

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
+5  A: 
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();
}
Anthony Pegram
Nice format-fix on line #1
sshow
@Joel, that's simply genius. Thanks.
Anthony Pegram