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?
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.
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.
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.
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.
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
No, you will need to manually delete the file. Path.GetTempPath() just gives you the folder path to the temp folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With