Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are files in the temporary folder automatically deleted?

If I create some file using Path.GetTempPath() - does it automatically get deleted at some stage, or is it up to me to delete it?

like image 861
Yaron Naveh Avatar asked Feb 14 '10 18:02

Yaron Naveh


People also ask

How long do files stay in temp folder?

You can easily delete these temporary folders to free up space on your computer. But using the Disk Cleanup tool only deletes temporary files that are older than seven days. Even the new Storage Sense feature in Windows 10 won't automatically clear temp files all the time.

Does Windows automatically delete temp files?

With Windows 10 Fall Creators Update (version 1709), Microsoft introduced a new option in Storage sense to automatically delete files from the Downloads folder or delete temp files that are not using.

Why are TMP files not deleting automatically?

So why do those files not get automatically deleted? First, if Windows shuts down improperly, the contents of the Temp folder may not be emptied. Also, some applications, such as synchronization software for handheld devices, use the Temp folder and may not delete all the files they leave behind.

Where do files go after temp folder?

Temp files often have the extension . TMP and are stored in the C:\Users\AppData\Local\Temp folder. If you're working on a document, your word-processing app may create a temporary file to track your progress. If the app crashes, you'll be able to recover your data from the temp file.


2 Answers

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
like image 56
finnw Avatar answered Sep 28 '22 02:09

finnw


No, you will need to manually delete the file. Path.GetTempPath() just gives you the folder path to the temp folder.

like image 28
Jason Evans Avatar answered Sep 28 '22 01:09

Jason Evans