I have written a motion detection winform c# desktop app.
The motion frames are saved as individual jpegs to my hard drive.
There are 4 cameras I record from. This is represented by the variable:
camIndex
Each jpeg's is under a file structure:
c:\The Year\The Month\The Day\The Hour\The Minute
to ensure the directories did not get too many files in each one.
The intention is for my app to be be running 24/7. The app can stop for reasons such as system reboot or that the User chooses to temporarily close it down.
At the moment I have a timer than runs every 5 minutes to delete files that are say more than 24 hours old.
I have found that my following code is memory intensive and over a period of a few days the explorer.exe has climbed in the RAM memory.
My motion app needs to be on all the while so a low memory footprint is essential for this 'archiving'...
The following code is long and seems to me hugely inefficient. Is there a better way I can achieve my aims?
I use this code:
List<string> catDirs = Directory.EnumerateDirectories(Shared.MOTION_DIRECTORY, "*", SearchOption.TopDirectoryOnly).ToList();
for (int index = 0; index < catDirs.Count; index++)
{
for (int camIndex = 0; camIndex < 4; camIndex++)
{
if (Directory.Exists(catDirs[index] + "\\Catalogues\\" + camIndex.ToString()))
{
List<string> years = GetDirectoryList(catDirs[index] + "\\Catalogues\\" + camIndex.ToString(), true);
if (years.Count == 0)
{
Directory.Delete(catDirs[index]);
}
for (int yearIndex = 0; yearIndex < years.Count; yearIndex++)
{
DirectoryInfo diYear = new DirectoryInfo(years[yearIndex]);
List<string> months = GetDirectoryList(years[yearIndex], true);
if (months.Count == 0)
{
Directory.Delete(years[yearIndex]);
}
for (int monthIndex = 0; monthIndex < months.Count; monthIndex++)
{
DirectoryInfo diMonth = new DirectoryInfo(months[monthIndex]);
List<string> days = GetDirectoryList(months[monthIndex], true);
if (days.Count == 0)
{
Directory.Delete(months[monthIndex]);
}
for (int dayIndex = 0; dayIndex < days.Count; dayIndex++)
{
DirectoryInfo diDay = new DirectoryInfo(days[dayIndex]);
List<string> hours = GetDirectoryList(days[dayIndex], true);
if (hours.Count == 0)
{
Directory.Delete(days[dayIndex]);
}
for (int hourIndex = 0; hourIndex < hours.Count; hourIndex++)
{
DirectoryInfo diHour = new DirectoryInfo(hours[hourIndex]);
List<string> mins = GetDirectoryList(hours[hourIndex], false);
if (mins.Count == 0)
{
Directory.Delete(hours[hourIndex]);
}
for (int minIndex = 0; minIndex < mins.Count; minIndex++)
{
bool deleteMe = false;
DirectoryInfo diMin = new DirectoryInfo(mins[minIndex]);
DateTime foundTS = new DateTime(Convert.ToInt16(diYear.Name), Convert.ToInt16(diMonth.Name), Convert.ToInt16(diDay.Name),
Convert.ToInt16(diHour.Name), Convert.ToInt16(diHour.Name), 00);
double minutesElapsed = upToDateDate.Subtract(foundTS).TotalMinutes;
if (minutesElapsed > diff)
{
deleteMe = true;
}
if (deleteMe)
{
Directory.Delete(mins[minIndex], true);
}
}
}
}
}
}
}
}