Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do files created with Path.GetTempFileName get cleaned up automatically?

Tags:

I've always assumed the answer is yes, but now I'm trying to find the truth.

When I create a temp file using Path.GetTempFileName(), will windows automatically clean that up later?

What about if I create a directory under Path.GetTempPath()? Will windows clean it up?

Or is it the developer's responsibility to delete files created there?

like image 541
Ronnie Overby Avatar asked Feb 08 '12 21:02

Ronnie Overby


People also ask

Are files in temp folder automatically deleted?

Replies (7)  From its name , temp file contains temporary files and they will be deleted.

How often does temp folder get cleared?

You can have Windows 10 clean it every day, every two weeks, every month, and every two months. The time period after which the temporary files are deleted works follows the setting for Storage Sense. Storage sense can delete files every day, every week, every month, or when you're low on disk space.


1 Answers

No they do not get deleted automatically. In order to create a file that will be deleted automatically when it is closed, pass FILE_FLAG_DELETE_ON_CLOSE to CreateFile.

The file is to be deleted immediately after all of its handles are closed, which includes the specified handle and any other open or duplicated handles. If there are existing open handles to a file, the call fails unless they were all opened with the FILE_SHARE_DELETE share mode. Subsequent open requests for the file fail, unless the FILE_SHARE_DELETE share mode is specified.

In order to gain access to this Win32 functionality from .net, use the SafeFileHandle class.

like image 104
David Heffernan Avatar answered Oct 05 '22 06:10

David Heffernan