windows 避免在读取文件时更新上次访问的日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5663766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid updating last-accessed date/time when reading a file
提问by Anodyne
We're building a Windows-based application that traverses a directory structure recursively, looking for files that meet certain criteria and then doing some processing on them. In order to decide whether or not to process a particular file, we have to open that file and read some of its contents.
我们正在构建一个基于 Windows 的应用程序,它递归地遍历目录结构,查找满足特定条件的文件,然后对它们进行一些处理。为了决定是否处理特定文件,我们必须打开该文件并读取其中的一些内容。
This approach seems great in principle, but some customers testing an early version of the application have reported that it's changing the last-accessed time of large numbers of their files (not surprisingly, as it is in fact accessing the files). This is a problem for these customers because they have archive policies based on the last-accessed times of files (e.g. they archive files that have not been accessed in the past 12 months). Because our application is scheduled to run more frequently than the archive "window", we're effectively preventing any of these files from ever being archived.
这种方法原则上看起来不错,但一些测试应用程序早期版本的客户报告说,它正在更改大量文件的上次访问时间(不足为奇,因为它实际上是在访问文件)。这对这些客户来说是个问题,因为他们有基于文件最后访问时间的归档策略(例如,他们归档在过去 12 个月内未被访问的文件)。因为我们的应用程序被安排比存档“窗口”更频繁地运行,所以我们有效地阻止了这些文件中的任何一个被存档。
We tried adding some code to save each file's last-accessed time before reading it, then write it back afterwards (hideous, I know) but that caused problems for another customer who was doing incremental backups based on a file system transaction log. Our explicit setting of the last-accessed time on files was causing those files to be included in every incremental backup, even though they hadn't actually changed.
我们尝试添加一些代码来保存每个文件在读取之前的上次访问时间,然后在之后将其写回(可怕,我知道)但这给正在根据文件系统事务日志进行增量备份的另一个客户造成了问题。我们对文件上次访问时间的显式设置导致这些文件包含在每个增量备份中,即使它们实际上并没有改变。
So here's the question: is there any way whatsoever in a Windows environment that we can read a file without the last-accessed time being updated?
所以这里的问题是:在 Windows 环境中,有没有什么方法可以让我们在不更新上次访问时间的情况下读取文件?
Thanks in advance!
提前致谢!
EDIT: Despite the "ntfs" tag, we actually can't rely on the filesystem being NTFS. Many of our customers run our application over a network, so it could be just about anything on the other end.
编辑:尽管有“ntfs”标签,但我们实际上不能依赖于 NTFS 的文件系统。我们的许多客户通过网络运行我们的应用程序,因此它可以是另一端的任何东西。
回答by Luke
The documentationindicates you can do this, though I've never tried it myself.
该文件表明,你可以做到这一点,虽然我从来没有尝试过自己。
To preserve the existing last access time for a file even after accessing a file, call SetFileTime immediately after opening the file handle with this parameter's FILETIME structure members initialized to 0xFFFFFFFF.
要在访问文件后保留文件的现有上次访问时间,请在打开文件句柄后立即调用 SetFileTime,并将此参数的 FILETIME 结构成员初始化为 0xFFFFFFFF。
回答by Suresh
From Vista onwards NTFS does not update the last access time by default. To enable this see http://technet.microsoft.com/en-us/library/cc959914.aspx
从 Vista 开始,NTFS 默认不更新上次访问时间。要启用此功能,请参阅http://technet.microsoft.com/en-us/library/cc959914.aspx
Starting NTFS transaction and rolling back is very bad, and the performance will be terrible.
启动NTFS事务并回滚非常糟糕,性能会很糟糕。
You can also do
你也可以这样做
FSUTIL behavior set disablelastaccess 0
FSUTIL 行为设置 disablelastaccess 0
回答by Jim
I don't know what your client minimum requirements are, but have you tried NTFS Transactions? On the desktop the first OS to support it was Vista and on the server it was Windows Server 2008. But, it may be worth a look at.
我不知道您的客户最低要求是什么,但您是否尝试过 NTFS 交易?在桌面上,第一个支持它的操作系统是 Vista,在服务器上是 Windows Server 2008。但是,它可能值得一看。
Start an NTFS transaction, read your file, rollback the transaction. Simple! :-). I actually don't knowif it will rollback the Last Access Date though. You will have to test it for yourself.
启动 NTFS 事务,读取您的文件,回滚事务。简单的!:-)。我实际上不知道它是否会回滚上次访问日期。您必须亲自测试它。
Here is a link to a MSDN Magazine article on NTFS transactions which includes other links. http://msdn.microsoft.com/en-us/magazine/cc163388.aspx
这是 MSDN 杂志上有关 NTFS 事务的文章的链接,其中包括其他链接。http://msdn.microsoft.com/en-us/magazine/cc163388.aspx
Hope it helps.
希望能帮助到你。