How to create a temporary file (0x100) to speed up the application

I have seen that Windows uses temporary files to improve the performance of some tasks. These files are marked with attribute 0x100 when I look at them. I have the following text from Microsoft: "

Using CreateFile () using the FILE_ATTRIBUTE_TEMPORARY flag, you let the system know that the file is likely to be short. a temporary file is created as a regular file. The system needs to perform the minimum number of lazy entries in the file system storage disk structures (directories, etc.). This gives the file was written to disk. "

Any example of creating such a temporary file using Delphi?

Thanks.

[EDIT]

An additional question: what can be the context of using such a file, for example, can it be used for a log system. is the log file with the temp attribute? Would it be faster and less memory when the log becomes very large?

[EDIT]

Ok, I created the file using the solution indicated by schnaader below with the FILE_ATTRIBUTE_TEMPORARY file:

hMyFile := CreateFile(FileName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0); 

Such a file receives the 0x120 attribute upon creation. Thus, a temporary file in accordance with the system.

I also create a file with the flag FILE_FLAG_DELETE_ON_CLOSE ( see this article by L. Osterman ).

So:

 hMyFile := CreateFile(FileName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0); 

This file does not receive an attribute, and the file is automatically deleted when the application is closed or destroyed.

I did not find how to combine attribute and flag . Any ideas?

thanks

+6
delphi temporary-files
source share
2 answers

Well, what about using the CreateFile () method?

 var FileName : PChar; hMyFile : THandle; ... hMyFile := CreateFile(FileName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0); if (hMyFile = INVALID_HANDLE_VALUE) then begin // Error end; ... CloseHandle(hMyFile); 

To combine the flag with FILE_FLAG_DELETE_ON_CLOSE , use or :

 hMyFile := CreateFile(FileName, GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY or FILE_FLAG_DELETE_ON_CLOSE, 0); 
+7
source share

The temporary flag simply indicates that you want to write to disk as late as possible. Most likely, you will want to write a log file, since you want the contents of the file to be written safely to disk so that you can access it even in the event of a system crash.

To quote the MSDN page for CreateFile () :

Specifying the FILE_ATTRIBUTE_TEMPORARY attribute causes file systems to not write data back to mass storage if there is sufficient cache, because the application deletes the temporary file after the handle is closed. In this case, the system can completely avoid writing data. Although it does not directly control data caching in the same way as the previously mentioned flags, the FILE_ATTRIBUTE_TEMPORARY attribute tells the system to store as much as possible in the system cache without writing and, therefore, can be troubling for certain applications.

In my opinion, you would use it only if it is really temporary data that you are going to delete in any case after you finish with it, for example. things you have to write to the %TEMP% directory.

+2
source share

All Articles