I need to work with some temporary files in my Windows Forms .NET 3.5 application. Those files are opened in an external application that can of course be running for a longer time than my own program.
Are there any best practices to make sure these temporary files are cleaned up at any time in order to avoid filling the user’s hard disk with "junk" files which aren’t needed anymore? Or does even Windows kind of handle this automatically?
A nice example is any mail client: When you open an attachment in any application, it is usually written to a temporary file which is opened. Is there a way to figure out how those files manage cleanup?
Using Google has shown me many, many cleanup and tune-up tools to clean the temp directory by hand, but I’d not like to force the users to do so. 🙂
If you attempt to deterministically clear the contents of a Temporary Files type folder, you risk removing files that are in use by other processes. The Windows operating system provides tools to allow users to remove those files when the volume’s available disk space reaches a certain threshold.
Now, if you can determine that after you use a specific temporary file that it will no longer be needed, then there’s no down-side to deleting that specific file.