Hi there,
I have to create a class that will monitor a given folder and delete all files older that X number of days using namespace "Files_ExtL ib1"
There are 2 methods I have to create. DeleteFilesOlde rThan() and FilesOlderThan( ) and have first method call the second one.
I have to follow the following structure..
I personally think it would make it easier if i used a DateTime structure to represent the date and time. I think it just makes the following code easier to do.
but i need to follow the structure...any help will be appreciated Thanks!!!
I have to create a class that will monitor a given folder and delete all files older that X number of days using namespace "Files_ExtL ib1"
There are 2 methods I have to create. DeleteFilesOlde rThan() and FilesOlderThan( ) and have first method call the second one.
I have to follow the following structure..
Code:
using System;
namespace Files_ExtLib1
{
/// <summary>
/// Summary description for FolderWatcher.
/// </summary>
public class FolderWatcher
{
string m_FolderPath;
public FolderWatcher()
{
//
// TODO: Add constructor logic here
//
}
#region Property
public string FolderPath
{
get
{
return m_FolderPath;
}
set
{
m_FolderPath = value;
}
}
#endregion
public int DeleteFilesOlderThan(int Days, int Hrs, int Mins)
{
}
public string[] FilesOlderThan(int Days, int Hrs, int Mins)
{
}
Code:
public static DateTime GetRelativeDateTime(int days, int hours, int minutes)
{
return DateTime.Now.AddDays(-days).AddHours(-hours).AddMinutes(-minutes);
}
Then using the relative DateTime, you can filter out files fairly easily:
public static string[] FilesOlderThan(DateTime dt)
{
DirectoryInfo directory = new DirectoryInfo(Directory.GetCurrentDirectory());
FileInfo[] files = directory.GetFiles(); //get FileInfos for the files in the current directory
ArrayList older = new ArrayList(); //list to hold the result
foreach(FileInfo file in files)
{
if(file.CreationTime < dt) //if smaller (older) than the relative time
older.Add(file.Name); //add it to the list
}
return (string[])older.ToArray(typeof(string)); //return as an array
}
Comment