文件创建日期时间针对不同文件和时间重复
我编写了一项服务,用于监视来自扫描仪的文件的文件放置位置。扫描程序会删除具有完全相同文件名的所有文件(例如 Test.tif),除非该文件已存在,然后在末尾附加时间戳(例如 Test_0809200915301900.tif)。
因此,当我处理这些文件时,我将一个“标签”附加到数据库条目以反映此特定文件,即文件名加上文件创建时间戳(以刻度为单位)。每个扫描仪最多可以每隔几秒进行一次扫描,因此精确到秒就足够了。
下面是生成这个所谓的唯一标签的代码:
FileInfo fileInfo = new FileInfo(filePath);
string tag = string.Format("{0}_{1}", filename,
fileInfo.CreationTimeUtc.Ticks.ToString());
生成的标签看起来像这样: Test1.tif_633931295923017954
由于某种原因,当一堆扫描来自同一个扫描仪时,比如说在 20 年内秒(例如,1 次扫描,然后 5 秒后再扫描一次,然后 5 秒后再扫描一次,等等),它们将获得完全相同的文件创建时间戳。
例如。
- 文件位于:Test1.tif
- 使用标签
Test1.tif_633931295923017954
拾取并存储 - Test1.tif 被删除。
- 文件输入:Test1.tif(5 秒后)
- 拾取但无法存储,因为生成的标记与
Test1.tif_633931295923017954
重复,
这怎么可能?蜱虫是相同的。我检查了创建时间对象,它也是相同的,尽管我实际看到它是在第一个对象创建 5 秒后创建的。
编辑:任何人都可以推荐一个解决方案来确保我处理的是唯一文件吗?我认为文件名+创建时间戳应该是一个足够好的检查,但显然事实并非如此。我无法关闭 Windows 正在执行的“隧道”功能。
编辑:我最终让流程重命名每个文件并附加一个 guid。然后处理文件的进程会查找仅附加了 guid 的文件。这确保了仅处理唯一的文件。
I have written a service that monitors a file drop location for files from a scanner. The scanner drops all files with the exact same file name (eg. Test.tif) unless that file already exists and then it appends on a timestamp on the end (eg. Test_0809200915301900.tif).
So when I process these files I attach a 'tag' to the db entry to reflect this specific file which is the filename plus the file creation timestamp in ticks. Each scanner can produce 1 scan every few seconds at best so precision to the second is sufficient.
Here is the code that generates this supposedly unique tag:
FileInfo fileInfo = new FileInfo(filePath);
string tag = string.Format("{0}_{1}", filename,
fileInfo.CreationTimeUtc.Ticks.ToString());
The generated tag would look something like: Test1.tif_633931295923017954
For some reason though when a bunch of scans come in from the same scanner say over the course of 20 seconds (eg. 1 scan, then 5 seconds later another, then 5 seconds later another, etc) the are getting the exact same file creation time stamp.
Eg.
- File in: Test1.tif
- Picked up and stored with tag
Test1.tif_633931295923017954
- Test1.tif is deleted.
- File in: Test1.tif (5 seconds later)
- Picked up and fail to be stored because generated tag is a duplicate with
Test1.tif_633931295923017954
How is this possible? The ticks are identical. I inspected the creation time object and it is identical as well even though I physically saw it created 5 seconds after the first one.
Edit: Can anyone recommend a solution to ensuring I am dealing with a unique file? I thought that filename + creation timestamp should be a good enough check but obviously it is not. I don't have the ability to turn off the 'Tunnelling' functionality that Windows is preforming.
Edit: I ended up having the process rename each file and appending a guid. The process that then processed the files looked for files with the guid attached only. This ensured only unique files were processed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Windows 的一项功能,称为文件系统隧道,并且是预期行为。本质上,这是一种操作系统,用于在用户编辑和保存文件时删除文件并写入新文件的软件。用户期望编辑后的文件的文件创建时间与原始文件相同,并且该软件可能要求文件名的“短”版本在编辑前后保持相同,因此 Windows 会伪造它。
有关更多详细信息,请参阅:文件系统隧道的杜撰历史了解更多详情。
编辑:
我很惊讶 Feaderne 建议的
FileSystemInfo.LastWriteTime
不起作用。您能否按原样保留
File1.tif
并使用FileSystemWatcher
监视目录中唯一文件的创建,对新创建的文件进行模式匹配,然后使用您的命名方案?This is a feature of Windows called File System Tunneling, and is expected behavior. Essentially this is an operating system working around for software that deletes a file and writes a new one when a user edits and save a file. The user expects the file creation time of the edited file to be the same as the original, and the software may require that the "short" version of the file name to remain the same before and after the edit so Windows fakes it.
For more details, please see: The apocryphal history of file system tunnelling for more details.
Edit:
I'm surprised that
FileSystemInfo.LastWriteTime
as Feaderne suggested didn't work.Can you leave
File1.tif
as-is and watch for the creation of the unique files in the directory usingFileSystemWatcher
, pattern match the newly created files, and copy them using your naming scheme?LastWriteTime
会给你你想要的时间戳吗?Would
LastWriteTime
give you the timestamp you want?