I'm trying to delete files in a directory that are older than 10 days, based on the "Last Modified Date". What it should do is take the input from a moxiemanager upload, which it does, and input that into the database, which it does. It should then delete files older than 10 days, which it doesn't. The following is my code:
public ActionResult CreateMain(BulletinsViewModel viewModel)
{
if (ModelState.IsValid)
{
BulletinsContext.tblBulletins.Add(new tblBulletin
{
ID = viewModel.BulletinID,
BulletinDisplayDate = viewModel.BulletinDisplayDate,
BulletinFilename = viewModel.MainBulletinName
});
DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/Files/Bulletins"));
FileSystemInfo[] FSI = DI.GetFiles();
foreach (FileSystemInfo fInfo in FSI)
{
if (fInfo.Extension == ".pdf")
{
DateTime dt = DateTime.Now.AddDays(-10);
if (fInfo.CreationTime < dt)
try
{
fInfo.Delete();
}
catch { }
}
}
BulletinsContext.SaveChanges();
return RedirectToAction("MainBulletins");
}
return View(viewModel);
}
I'm thinking possibly that it may be either the filepath isn't the correct syntax for this method, or the "fInfo.CreationTime
" section is wrong, but I feel like CreationTime would be last modified?