如何创建临时文件(0x100)以加速应用程序

发布于 2024-07-25 23:09:47 字数 1390 浏览 6 评论 0原文

我见过Windows系统使用临时文件来提高某些任务的性能。 当我查看这些文件时,它们被标记为 0x100 属性。 我从 Microsoft 收到以下文本:“

通过使用 CreateFile() 和 FILE_ATTRIBUTE_TEMPORARY 标志,你让 系统知道该文件是 可能是短暂的。 这 像平常一样创建临时文件 文件。 系统需要做最少的事情 文件的惰性写入量 系统保存磁盘结构 (目录等)一致。 这看起来是 文件已写入磁盘 ”

使用 Delphi 创建此类临时文件的任何示例?

谢谢。

[编辑]

补充问题:使用此类文件的上下文可能是什么,例如,它是否可以用于日志系统。日志是具有 temp 属性的此文件?当日志变得非常大时,它会更快且更少内存吗?

[编辑]

好的,我使用下面 schnaader 给出的解决方案和 FILE_ATTRIBUTE_TEMPORARY 创建文件:

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

这样的文件在创建时会获得 0x120 属性。

我还创建了一个带有 FILE_FLAG_DELETE_ON_CLOSE 标志的文件(请参阅这篇文章作者:L. Osterman

所以:

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

该文件没有属性,并且当应用程序关闭或销毁时该文件会自动删除。

我没有找到如何组合属性和标志。有什么想法吗

I have seen that Windows system use temporary files to increase the performance of some tasks. Those files are marked with the 0x100 attribute when i look at them. I have got the following text from Microsoft: "

By using CreateFile() with the
FILE_ATTRIBUTE_TEMPORARY flag, you let
the system know that the file is
likely to be short lived. The
temporary file is created as a normal
file. The system needs to do a minimal
amount of lazy writes to the file
system to keep the disk structures
(directories and so forth) consistent.
This gives the appearance that the
file has been written to the disk
."

Any example of creating such temporary file using Delphi?

Thanks.

[EDIT]

Complementary question: what could be the context of using such file, example, could it be used for a log system. the log being this file with the temp attribute? Would it be faster and less memory prone when log get very large?

[EDIT]

Ok i have create file using solution given by schnaader below with the FILE_ATTRIBUTE_TEMPORARY:

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

Such file get the 0x120 attribute when created. Thus a temporary file according to system.

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

So:

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

This file gets no attribute and the file is automatically deleted when the application is closed or destroyed.

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2024-08-01 23:09:47

那么,使用 CreateFile() 方法怎么样?

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);

要将标志与 FILE_FLAG_DELETE_ON_CLOSE 结合使用,请使用 or

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

Well, how 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); 
无人接听 2024-08-01 23:09:47

临时标志仅表明您希望尽可能晚地写入磁盘。 这与您想要的日志文件完全相反,因为您希望将文件的内容安全地写入磁盘,以便即使在系统出现故障时也可以访问它。

引用 CreateFile() 的 MSDN 页面:

指定 FILE_ATTRIBUTE_TEMPORARY 属性会导致文件系统在有足够的缓存内存可用时避免将数据写回大容量存储,因为应用程序会在句柄关闭后删除临时文件。 在这种情况下,系统可以完全避免写入数据。 虽然它不像前面提到的标志那样直接控制数据缓存,但 FILE_ATTRIBUTE_TEMPORARY 属性确实告诉系统在系统缓存中保存尽可能多的数据而不进行写入,因此可能是关注某些应用程序。

在我看来,只有当它确实是临时数据并且在使用完后无论如何都要删除时,您才会使用它,例如您写入 %TEMP% 目录的内容。

The temporary flag just indicates that you'd like the write to the disk to be as late as possible. This is quite the contrary you'd want for a log file, as you'd like to get the content of the file written safly to disk so you can access it even in case of a system failure.

To quote from the MSDN page fro CreateFile():

Specifying the FILE_ATTRIBUTE_TEMPORARY attribute causes file systems to avoid writing data back to mass storage if sufficient cache memory is available, because an application deletes a temporary file after a handle is closed. In that case, the system can entirely avoid writing the data. Although it doesn't directly control data caching in the same way as the previously mentioned flags, the FILE_ATTRIBUTE_TEMPORARY attribute does tell the system to hold as much as possible in the system cache without writing and therefore may be of concern for certain applications.

In my opinion, you'd only use it, if it really is temporary data which you are going to delete anyways after you are done with it, e.g. things you'd write to %TEMP% directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文