December 19, 2008

How to create a file that will be deleted automatically after close its handle?

Use CreateFile() API with the flag: FILE_FLAG_DELETE_ON_CLOSE

If we want to create a file when the process starts and delete it when the process exits, CreateFile() is convenient to you.

FILE_FLAG_DELETE_ON_CLOSE means the file will be deleted automatically after close hanlde. Even if the process was crashed, it would also be deleted.

Reference in MSDN: