c Deletes files from the specified folder before today

  • 2021-12-05 07:03:33
  • OfStack

1. Description

Use the Directory class to delete files under the specified folder before today or earlier.

2. Code


// Folder path 
string strFolderPath = Server.MapPath("~") + "\\excel\\";
DirectoryInfo dyInfo = new DirectoryInfo(strFolderPath);
// Get all the files in the folder 
foreach (FileInfo feInfo in dyInfo.GetFiles())
{
  // Determine whether the file date is less than today, and delete it if it is 
  if (feInfo.CreationTime < DateTime.Today)
    feInfo.Delete();
}

Related articles: