读取文件时避免更新上次访问的日期/时间

发布于 2024-11-01 10:57:41 字数 566 浏览 8 评论 0原文

我们正在构建一个基于 Windows 的应用程序,它递归地遍历目录结构,查找满足特定条件的文件,然后对它们进行一些处理。为了决定是否处理特定文件,我们必须打开该文件并读取它的一些内容。

这种方法原则上看起来很棒,但一些测试该应用程序早期版本的客户报告说,它正在更改大量文件的上次访问时间(毫不奇怪,因为它实际上正在访问这些文件)。这对于这些客户来说是一个问题,因为他们的归档策略基于文件的上次访问时间(例如,他们归档过去 12 个月内未访问过的文件)。由于我们的应用程序计划比存档“窗口”更频繁地运行,因此我们有效地防止了任何这些文件被存档。

我们尝试添加一些代码来在读取每个文件之前保存其上次访问时间,然后将其写回(我知道这很可怕),但这给另一个基于文件系统事务日志进行增量备份的客户带来了问题。我们对文件上次访问时间的显式设置导致这些文件包含在每个增量备份中,即使它们实际上没有更改。

那么问题来了:在 Windows 环境中,有没有什么方法可以让我们读取文件而不更新上次访问时间?

提前致谢!

编辑:尽管有“ntfs”标签,我们实际上不能依赖 NTFS 文件系统。我们的许多客户通过网络运行我们的应用程序,因此另一端可以是任何东西。

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.

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.

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?

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.

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

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

发布评论

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

评论(4

漫雪独思 2024-11-08 10:57:41

文档表明您可以执行以下操作这个,虽然我自己从未尝试过。

要在访问文件后保留文件的现有上次访问时间,请在打开文件句柄后立即调用 SetFileTime,并将此参数的 FILETIME 结构成员初始化为 0xFFFFFFFF。

The documentation indicates 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.

放低过去 2024-11-08 10:57:41

从 Vista 开始,NTFS 默认情况下不会更新上次访问时间。要启用此功能,请参阅 http://technet.microsoft.com/en-us/library /cc959914.aspx

启动NTFS事务并回滚是非常糟糕的,性能会很糟糕。

您还可以执行

FSUTILbehavior setdisablelastaccess 0

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

Starting NTFS transaction and rolling back is very bad, and the performance will be terrible.

You can also do

FSUTIL behavior set disablelastaccess 0

淡莣 2024-11-08 10:57:41

我不知道您的客户端最低要求是什么,但是您尝试过 NTFS 事务吗?在桌面上,第一个支持它的操作系统是 Vista,在服务器上,它是 Windows Server 2008。但是,它可能值得一看。

启动 NTFS 事务、读取文件、回滚事务。简单的! :-)。我实际上不知道它是否会回滚上次访问日期。您必须亲自测试一下。

以下是有关 NTFS 事务的 MSDN 杂志文章的链接,其中包括其他链接。 http://msdn.microsoft.com/en-us/magazine/cc163388.aspx

希望有帮助。

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.

Start an NTFS transaction, read your file, rollback the transaction. Simple! :-). I actually don't know if it will rollback the Last Access Date though. You will have to test it for yourself.

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

Hope it helps.

断桥再见 2024-11-08 10:57:41

您还可以在将更改 LastAccessTime 的代码之前使用方法 File.GetLastAccessTime(fileInfoLocation); ,然后在将更改 LastAccessTime 的代码之后使用方法 File.SetLastAccessTime(fileInfoLocation, originalLastAccessTime); 将其设置回原来的状态,尽管您需要 SetLastAccessTime 部分的管理权限,如果您想将其更改为原始 AKA 以管理员身份运行
这里是一些代码示例:

FileInfo fileInfoLocation = new FileInfo(getAllExeLocation[l]);
                                                DateTime originalLastAccessTime = File.GetLastAccessTime(fileInfoLocation.FullName);//Save the LastAccessTime before it was changed by Icon.Extract Method
                                                exeIcon = Icon.ExtractAssociatedIcon(fileInfoLocation.FullName);//This changes LastAccessTime
                                                File.SetLastAccessTime(fileInfoLocation.FullName, originalLastAccessTime);//Set the LastAccessTime to the originalOne though it needs Privileges to do this one(run as admin) or Use Try-Catch statement

Also you can use the method File.GetLastAccessTime(fileInfoLocation); before the code that will change the LastAccessTime and then after the code thats going to change the LastAccessTime you can use the method File.SetLastAccessTime(fileInfoLocation, originalLastAccessTime); to set it back to what it was though you need Administrative privileges for the SetLastAccessTime part if you want to change it to the original AKA run as admin.
Heres some code for example:

FileInfo fileInfoLocation = new FileInfo(getAllExeLocation[l]);
                                                DateTime originalLastAccessTime = File.GetLastAccessTime(fileInfoLocation.FullName);//Save the LastAccessTime before it was changed by Icon.Extract Method
                                                exeIcon = Icon.ExtractAssociatedIcon(fileInfoLocation.FullName);//This changes LastAccessTime
                                                File.SetLastAccessTime(fileInfoLocation.FullName, originalLastAccessTime);//Set the LastAccessTime to the originalOne though it needs Privileges to do this one(run as admin) or Use Try-Catch statement
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文