在 C# 中检查文件的最后修改日期
我正在寻找一种方法来查看 C# 中文件上次修改的时间。我拥有该文件的完全访问权限。
I'm looking to find out a way of seeing when a file was last modified in C#. I have full access to the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
System.IO.File.GetLastWriteTime就是你所需要的。
System.IO.File.GetLastWriteTime is what you need.
您只需要
File.GetLastWriteTime< /code>
静态方法。
示例:
但请注意,在极少数情况下,写入文件时系统不会更新上次修改时间(这可能是为了高频写入的优化而故意发生的,例如日志记录,或作为错误),那么这方法将会失败,您将需要订阅系统的文件写入通知,不断监听。
You simply want the
File.GetLastWriteTime
static method.Example:
Note however that in the rare case the last-modified time is not updated by the system when writing to the file (this can happen intentionally as an optimisation for high-frequency writing, e.g. logging, or as a bug), then this approach will fail, and you will instead need to subscribe to file write notifications from the system, constantly listening.
请注意,函数 File.GetLastWriteTime 并不总是按预期工作,这些值有时不会由操作系统立即更新。即使文件之前已被修改,您也可能会得到旧的时间戳。
不同操作系统版本的行为可能有所不同。例如,这个单元测试每次在我的开发机器上都运行良好,但在我们的构建服务器上总是失败。
请参阅 File.GetLastWriteTime 似乎返回“过时” ' value
您的选择:
a) 接受偶尔的遗漏。
b) 构建一个实现观察者模式的主动组件(例如,tcp 服务器客户端结构),直接传达更改而不是写入/读取文件。快速且灵活,但另一个依赖性和可能的故障点(当然还有一些工作)。
c) 通过替换其他进程定期读取的专用信号文件的内容来确保信令进程。它并不那么聪明,因为它是一个轮询过程,并且比调用 File.GetLastWriteTime 具有更大的开销,但如果不经常检查来自太多地方的内容,它会完成工作。
对其进行一些测试:
Be aware that the function File.GetLastWriteTime does not always work as expected, the values are sometimes not instantaneously updated by the OS. You may get an old Timestamp, even if the file has been modified right before.
The behaviour may vary between OS versions. For example, this unit test worked well every time on my developer machine, but it always fails on our build server.
See File.GetLastWriteTime seems to be returning 'out of date' value
Your options:
a) live with the occasional omissions.
b) Build up an active component realising the observer pattern (eg. a tcp server client structure), communicating the changes directly instead of writing / reading files. Fast and flexible, but another dependency and a possible point of failure (and some work, of course).
c) Ensure the signalling process by replacing the content of a dedicated signal file that other processes regularly read. It´s not that smart as it´s a polling procedure and has a greater overhead than calling File.GetLastWriteTime, but if not checking the content from too many places too often, it will do the work.
Some tests for it:
只需使用 File.GetLastWriteTime 即可。该页面上有一个示例,展示了如何使用它。
Just use File.GetLastWriteTime. There's a sample on that page showing how to use it.
实际的最后修改日期时间位于 Microsoft.VisualBasic.FileSystem.FileDateTime(pathString) 中。
如果文件是从其原始位置复制的,
LastWriteTime
会给您一个误报。请参阅 https://learn .microsoft.com/en-us/dotnet/api/microsoft.visualbasic.filesystem.filedatetime?view=netframework-4.8
另外:我注意到计算机之间传输的某些文件的修改日期时间可能会有所不同左右。 NFI 为什么。我在支票中添加了 5 秒的捏造因素来解释这种差异。
The actual real last modified DateTime is in
Microsoft.VisualBasic.FileSystem.FileDateTime(pathString)
.LastWriteTime
will give you a false positive if the file has been copied from its original location.See https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.filesystem.filedatetime?view=netframework-4.8
Also: I have noticed that some files transferred between computers can differ in their modified DateTime by a second or so. NFI why. I added a 5-second fudge-factor to my check to account for this discrepancy.