This is what you described:

private void DeleteFilesOlderThan20Days()
{
        DateTime TwentyDaysAgo = DateTime.Now.AddDays(-20);
        DirectoryInfo di = new DirectoryInfo(@"C:\temp");
        foreach (FileInfo fi in di.GetFiles())
        {
                if(fi.CreationTime < TwentyDaysAgo)
                {
                        fi.Delete();
                }
        }
}

I don't know of any other way to get the job done.

--b

Bryan Batchelder 
eBusiness Consultant 
ConnectWise, Inc. 
813-935-7100 x 425 



> -----Original Message-----
> From: Blain Timberlake [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, June 04, 2002 8:38 AM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Deletion of old files....
> 
> 
> What is the cleanest way to delete all files before date X in 
> a directory?  Basically I'm going to set up a timer to clean 
> out files that are older than 20 days.  Up to now what I've 
> been doing is getting a list of all the files in the 
> directory and then systematically going through each one, and 
> looking at it's IO.FileInfo to look at the .CreationTime, do 
> a comparison and then do a .Delete() if necc.
> 
> Is there a better way to do this?
> =Blain
> 
> You can read messages from the DOTNET archive, unsubscribe 
> from DOTNET, or subscribe to other DevelopMentor lists at 
> http://discuss.develop.com.
> 

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to