To delete files from a specific folder you can use following code snippet written in c#.
More Details :
foreach (FileInfo fileInformation in new DirectoryInfo(@”I:/TrialTesting/”).GetFiles())
{
File.Delete(fileInformation.FullName);
}
However, you can delete files comparing “Date Modeified ” attribute .
Have a look at the following code snippet. This will delete all files
from folder except last five day’s files.
try
{
DateTime systemDate = DateTime.Now;
int DefaultDay = 5;
foreach (FileInfo fileInformation in new DirectoryInfo(@”I:/TrialTesting/”).GetFiles())
{
TimeSpan difference;
difference = systemDate - fileInformation.LastWriteTime;
//Delete Files from Folder
if (difference.Days >= DefaultDay)
{
File.Delete(fileInformation.FullName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
More Details :
Live Training in jaipur
No comments:
Post a Comment