3

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:

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.

I need to delete files that are more than 24hours old.

I use this code to accomplish that:

   Directory
        .GetFiles(Shared.MOTION_DIRECTORY, "*.*", SearchOption.AllDirectories)
        .Where(item =>
        {
            try
            {
                var fileInfo = new FileInfo(item);
                if (fileInfo.CreationTime < DateTime.Now.AddHours(-24))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        })
        .ToList()
        .ForEach(File.Delete);

and then I use this to delete the directories:

            Directory
          .GetDirectories(Shared.MOTION_DIRECTORY, "*.*", SearchOption.AllDirectories)
           .Where(item =>
           {
               try
               {
                   var dirInfo = new DirectoryInfo(item);
                   if (dirInfo.CreationTime < DateTime.Now.AddHours(-24) && !dirInfo.EnumerateFiles().Any())
                   {
                       return true;
                   }
                   else
                   {
                       return false;
                   }
               }
               catch (Exception)
               {
                   return false;
               }
           })
           .ToList()
           .ForEach(Directory.Delete);

but of course the error occurs when deleting a directory if there is a sub-directory (even of there are no files).

Is there a way of deleting the sub-folders automatically if empty or..?

Needless to say this has to be a low process operation because of the nature of my app.

Thanks

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

2 Answers2

2

Yes. Use the overload that allows recursive deletion.

Directory.Delete(path, true)

http://msdn.microsoft.com/en-us/library/fxeahc5f%28v=vs.110%29.aspx

spender
  • 117,338
  • 33
  • 229
  • 351
2

Use the overload with a bool and pass true to delete recursively. That will also prevent the IOException that is thrown.

.ForEach(f => Directory.Delete(f, true));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I have a lot of stuff to delete (4gb) will see what happens and report back -thanks – Andrew Simpson Jan 07 '15 at 10:47
  • @AndrewSimpson: [this is the related question](http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true). But don't you want to delete only empty directories anyway? – Tim Schmelter Jan 07 '15 at 10:52
  • sorry, poor checking by myself. I did not expect the code to work that fast. The error DID occur on an empty directory but the rest all deleted OK. I am not sure why on that 1 directory (which is empty) it failed. Either locked by explorer or has a hidden system file. So, not sure at the mo. But your suggestion 99.9% worked :) – Andrew Simpson Jan 07 '15 at 10:55