Are files in the temporary folder automatically deleted?
Basically if your application does not delete a file it will still be there until your application removes it and you should manage files your app creates based on that idea.
That said, once the file is closed you must always allow for the fact that it may not be there next time you want it and that you may need to recreate it. For example, Windows has a "disk cleanup tool" which may be run when space gets low, when directed by a user, or on a schedule...
Starting with Windows 10, the answer is posibly yes - depending on the machine configuration and the amount of free space on the drive hosting the TEMP folder.
Specifically, Storage Sense can arbitrarily delete files from the TEMP folder (I found that out the hard way) if enabled by the user. And from what I can tell, it will self-enable on low disk space.
No, you will need to manually delete the file. Path.GetTempPath()
just gives you the folder path to the temp folder.
FileOptions.DeleteOnClose
will cause the file to be deleted automatically when closed. This also works if the program is terminated by an exception.
For example, as mentioned in this answer:
using (FileStream fs = new FileStream(Path.GetTempPath() + "foo.bar",
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
4096, FileOptions.RandomAccess | FileOptions.DeleteOnClose))
{
// temp file exists
}
// temp file is gone