如何创建临时文件(0x100)来加速应用程序 [英] How to create temporary file (0x100) to accelerate application

查看:114
本文介绍了如何创建临时文件(0x100)来加速应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Windows系统使用临时文件来增加某些任务的性能。当我查看这些文件时,这些文件被标记为0x100属性。我有以下Microsoft的文本:


通过使用CreateFile()与
FILE_ATTRIBUTE_TEMPORARY标志,你让
系统知道文件是
可能是短暂的,
临时文件被创建为一个正常的
文件,系统需要做一个最小的
数量的懒惰写入文件
系统以保持磁盘结构
(目录等)一致。
这给出了
文件已写入磁盘的外观


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



谢谢。





补充问题:使用这样的文件可能是上下文,例如用于日志系统。日志是这个文件与temp属性?当日志变得非常大时,它会更快,内存容易减少吗?





确定我使用以下schnaader提供的解决方案创建文件,其中包含FILE_ATTRIBUTE_TEMPORARY: p>

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

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



我没有找到如何组合属性和标志。任何想法?



谢谢

解决方案

嗯,使用CreateFile方法?

  var 
FileName:PChar;
hMyFile:THandle;

...

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

if(hMyFile = INVALID_HANDLE_VALUE)然后开始
//错误
end;

...

CloseHandle(hMyFile);

要将标志与 FILE_FLAG_DELETE_ON_CLOSE 组合使用

  hMyFile:= CreateFile(FileName,
GENERIC_WRITE,
0,
nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY或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

解决方案

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

这篇关于如何创建临时文件(0x100)来加速应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆