RESOLVED delete a file older than thirty days I am getting a directory Invalid error
I placed this code yesterday which focuses on deleting a file older the thirty days and this process checks after my streamwriter code the whole thing looks like this :
Code:
public LoggingAction( GlobalConfiguration globalConfig, PayFilePackageCol paymentfilePackCol)
{
GlobalConfig = globalConfig;
PayFilePackCol = paymentfilePackCol;
logfilePath1 = this.GlobalConfig.Payment_Log_File_Loc;
String folder = Path.GetDirectoryName(logfilePath1);
String fileName = Path.GetFileNameWithoutExtension(logfilePath1);
String extension = Path.GetExtension(logfilePath1);
String newName = folder + "\\" + fileName + "_" + ISO_Date() + extension;
pSW = new StreamWriter(newName, true);
string[] files = Directory.GetFiles(newName);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.LastAccessTime < DateTime.Now.AddDays(-30))
fi.Delete();
}
}
~LoggingAction()
{
now the global config. Payment_Log_File_Loc is acutally C:\Temp on my local c drive so unless my permissions are set that I can't delete from there what else could be going on to fire off that exception ?
Re: deleting a file older than thirty days I am getting a directory Invalid error
Solved this myself later today. After lunch
Code:
string[] files = Directory.GetFiles(folder);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.LastWriteTime < DateTime.Now.Subtract(TimeSpan.FromDays(30)))
fi.Delete();
}
The last write time is what needs to be accessed in the file not the creation or accessed time wihich will usually be in sync date time now whenever the file is tampered with, moved etc.