8

I'm working on a program which will generate some temporary files, wait for the user's input on a few things, then use these temporary files for an operation.

I was wondering if I can reliably expect that these temporary files will not go away before I am completely done with them (e.g. will they disappear while the user is working?).

Obviously, I could create my own folder in my appdata and use that for temporary files. But it would be easier to use C#'s Path.GetTempFileName() or Path.GetTempPath() + somename. When would files created in this way be removed?

To clarify, I'm not looking for how to create temporary files, but rather how long temporary files created in GetTempPath() are kept, and whether or not that would be long enough to wait for user input before using them.

NickAldwin
  • 11,584
  • 12
  • 52
  • 67
  • 1
    Does the temp path get deleted at all? Mine contains files several months old, as old as this windows installation... – Jens Jul 01 '10 at 14:19
  • That's what I'm not sure about. See my comment on Tim's answer to see why I'm wary of just assuming that unless I get a concrete answer... – NickAldwin Jul 01 '10 at 14:28
  • Starting with Windows 10 and Storage Sense this is no longer guaranteed: https://stackoverflow.com/a/63848110/67824 – Ohad Schneider Sep 11 '20 at 13:51

5 Answers5

6

I'm not aware of any process in Windows that deletes temporary files automatically. The user may have a cleanup job set up, but you can reasonably expect your files to be left alone for a day or two as a minimum.

For comparison, when you open an attachment in Outlook, it copies the attachment to a temporary file and launches the associated application. These temporary attachment files might need to stay around indefinitely, if the user never closes the associated application.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • Well, it's reasonable to assume that anything automatically removing temporary files wouldn't/couldn't remove files that were currently open (like in your example); however, my files won't be held open by the application while the user is working. I asked this question because I've had cases where I open something temporary (similar to your Outlook example), then close it, then if I try to open the same temporary file again--just seconds after closing it--it's gone. This may be because the generating application is removing them, but I just wanted to make sure there wasn't something else. – NickAldwin Jul 01 '10 at 14:23
  • As a note, I accepted your answer, but I'm not actually saving to the temp directory in my program, as I forgot about another mode that would potentially leave files there for days, or weeks, or however long the user wants, before handling them -- so my own cache folder is a better option in this case, I think. But I still can't find any info saying that the temp files are autodeleted, so I'm thinking that was probably the program just being a proper software citizen and cleaning up after itself (which Outlook probably should do!) – NickAldwin Jul 01 '10 at 22:21
4

It's completely safe, and your instinct is 100% correct--this is the way to handle temp files, as long as you remember to clean them up afterwards.

Calling GetTempPath() does nothing more than point you to a safe place to put temporary files. It's the modern equivalent of getting the value of the environment variable PATH. The directory is never cleared automatically -- you'll often find it filled with ancient junk.

egrunin
  • 24,650
  • 8
  • 50
  • 93
3

It won't get cleaned up by itself.

Use Windows Disk Cleanup to do this.

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
2

As far as I am aware, the temporary files will linger around until either you remove then programmatically, or the user cleans them up (like using Disk Clean utility or manually deleting them).

When using temporary files, I always retain the filename created and make an attempt to delete the file when I'm done with it, otherwise I have noticed that I accrue large numbers of temporary files in the temp directory over a large timespan.

Dr Herbie
  • 3,930
  • 1
  • 25
  • 28
2

Agree with Tim. Atleast in XP, the responsibility of cleaning up the files from the temp folder resides with the program that put them there in the 1st place. Even with the newer operating systems like Vista / 7, i dont think there is any windows automatic cleanup happening for the %TMP% folder.

Jagmag
  • 10,283
  • 1
  • 34
  • 58