如何创建临时文件(0x100)以加速应用程序
我见过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,使用 CreateFile() 方法怎么样?
要将标志与
FILE_FLAG_DELETE_ON_CLOSE
结合使用,请使用or
:Well, how about using the CreateFile() method?
To combine the flag with
FILE_FLAG_DELETE_ON_CLOSE
, useor
:临时标志仅表明您希望尽可能晚地写入磁盘。 这与您想要的日志文件完全相反,因为您希望将文件的内容安全地写入磁盘,以便即使在系统出现故障时也可以访问它。
引用 CreateFile() 的 MSDN 页面:
在我看来,只有当它确实是临时数据并且在使用完后无论如何都要删除时,您才会使用它,例如您写入
%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():
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.