0

I write temporary files to a temporary folder like this:

string path = Path.GetTempPath() + "\\" + Path.GetFileName(originalFilePath);
File.WriteAllBytes(path, data);

I write large files of images. So, I have a question - a temporary folder is cleaned itself over time? Or do I need to when the application exit delete all temporary files?

PS: Sorry for my English.

Mikhail
  • 2,612
  • 3
  • 22
  • 37

1 Answers1

3

It is good practice to clean your temporary file at application exit. Create a class that manage temporary resources and use destructor to delete the temporary file(s).

public class TemporaryFile
{
    private string _fileName = String.Empty;

    <other stuffs...>

    ~TemporaryFile()
    {
        try
        {
            File.Delete(_fileName);
        }
        catch
        {
        }
    }
}