Click here to Skip to main content
Click here to Skip to main content
Go to top

A simple technique to take care of temporary files (Tip: Windows won't do it for you)

, 31 May 2010
Rate this:
Please Sign up or sign in to vote.
This is a nice and simple approach that I came up with in order to delete temporary files even long after I forgot about them.

Introduction/Background

Hello again!

Two days ago, I was working on a complex ASP.NET upload module. (I was actually basing my work on this half completed upload module - http://darrenjohnstone.net.)

I changed the code so it will automatically find out what the Windows Temp folder is, in number of ways so it will have a failsafe, and then upload files to a subfolder there, and give it a random name from a GUID. So far - so good.

Now the thing that bugged me all along the way is - what the heck will happen to the temp files?

You see the thing is that, Windows do not take care of the Temp folder in any way. It will never delete files by itself. An application that's making things dirty in the temp has to clean it after itself afterwards.

So I provided a "MoveTo" function to move the file to the destination location (not copy, that will be a waste of resources). This way, if the file is handled, nothing is left in the temp. But what if no code handled the uploaded files? Or there was an error? The file can be handled or can not be handled - anywhere during the current context, and I have no way to track/control what happens.

Then I came up with a simple solution: A deleter class.

It is actually very simple:

  1. It contains a full local path to a file which we want to delete.
  2. It implements a proper dispose pattern, which will dispose the file itself.
  3. It implements a method to "Cancel" the deletion.

OK, I'm going to write down the code that I wrote, but I'm not going to write anything regarding the upload mechanism because it is an unrelated issue, and this "deleter" class is relevant to many other situations.

Using the Code

This is the code:

public class TemporaryFileDeleter : IDisposable 
{
  private string _LocalFilePath;

  public string LocalFilePath
  {
      get { return _LocalFilePath; }
  }

  public TemporaryFileDeleter(string localFilePath)
  {
      _LocalFilePath = localFilePath;
  }

  ~TemporaryFileDeleter()
  {
      Dispose(false);
  }

  public void DeleteFile()
  {
      string path = _LocalFilePath;
      _LocalFilePath = null;
      if (path == null) return;
      System.IO.File.Delete(path);
  }

  public void DoNotDelete()
  {
      _LocalFilePath = null;
  }

  public void Dispose()
  {
      Dispose(true);
      GC.SuppressFinalize(this);
  }

  protected virtual void Dispose(bool disposing)
  {
      if (disposing)
      {
      }
      // Now clean up Native Resources
      try
      { 
          DeleteFile();
      }
      catch { }
  }
}

If you are not familiar with .NET's disposing mechanism, you better start researching it a bit, so you'll understand what's going on here. (It is actually very simple.)

What happens here is that only when this class is disposed, it tries to delete the file.

So, if there's a file that I created that is temporary and I want to get rid of it later on, I will create an instance of this class like this:

deleter = new TemporaryFileDeleter("C:\Windows\Temp\1234.dat");

But if I decided to keep the file (or prevent it from trying to delete a file that I have already moved, in case of a "MoveTo" function), I can call: deleter.DoNotDelete().

If I'll use again the example of a file upload module, then it goes like this: I'm creating a separate "File processor" class for each FileUpload (derived) control, and that class tries to create the temp file and stream data inside the file. But right after it creates the file, long before it starts streaming in, it will create an instance of the TemporaryFileDeleter and assign it as a member variable to the processor class. The garbage collector of .NET framework will dare to destroy/dispose the processor class only after the FileUpload control got destroyed, which means when the Request is finished. And only then it will also try to dispose the TemporaryFileDeleter class and actually delete the file.

If anywhere along the way something goes really wrong, it's still OK, because even if IIS terminates unexpectedly, it will still try to dispose objects and kill that file! I made some tests and caused IIS to crash - the temp files still got deleted.

Note: Even if you are not seeing the file getting deleted immediately, it will happen probably in the next 2-3 minutes. The garbage collector works in mysterious ways (smart ways), and it will kill the file when it wants to.

Points of Interest

A point of interest, or a point of driving you mad, is the fact that Microsoft never implemented any garbage collector for Windows... Why do I always have to clean up after other applications and after Windows itself? It will be very simple to decide that Windows will clean up files from Temp folder, on each restart (or maybe on a second restart to allow legacy support for setup applications which keep temp files until next restart). Maybe even a way to "register" a temp file with Windows, for deletion later, with some set of rules. But no! Haven't heard of such thing...

I hope I helped someone!

History

Nothing yet...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Cohen Gindi
Software Developer (Senior)
Israel Israel
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberdvptUml16-Apr-11 23:04 
GeneralMy vote of 1 PinmemberAndrew Glina13-Jun-10 23:28 
GeneralMy vote of 2 Pinmemberjiragabo8-Jun-10 19:08 
GeneralMy vote of 1 Pinmemberkevin_goff@gallup.com8-Jun-10 5:00 
GeneralMy vote of 1 PinmemberMurat YILMAZ7-Jun-10 20:58 
GeneralFileOptions.DeleteOnClose PinmemberDaniel Grunwald1-Jun-10 0:25 
GeneralRe: FileOptions.DeleteOnClose PinmemberDaniel Cohen Gindi1-Jun-10 0:28 
NewsWhy Microsoft never implemented any garbage collector for windows... PinmemberTommyTooth31-May-10 21:48 
GeneralEither that, or... PinmemberMathieu CARTOIXA31-May-10 20:49 
GeneralRe: Either that, or... PinmemberDaniel Cohen Gindi31-May-10 20:55 
GeneralRe: Either that, or... PinmemberPablo Aliskevicius7-Jun-10 21:59 
GeneralRe: Either that, or... PinmemberEnigmatic Texan8-Jun-10 17:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 31 May 2010
Article Copyright 2010 by Daniel Cohen Gindi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid