简体   繁体   English

文件关闭问题 windows

[英]File close issue windows

We are using File.WriteAllBytes to write data to the disk.我们正在使用 File.WriteAllBytes 将数据写入磁盘。 But if a reboot happens just about the time when we close the file, windows adds null to the file.但是,如果在我们关闭文件时重新启动,windows 会将 null 添加到文件中。 This seems to be happening on Windows 7. So once we come back to the file we see nulls in the file.这似乎发生在 Windows 7 上。因此,一旦我们回到文件,我们会在文件中看到空值。 Is there a way to prevent this.有没有办法防止这种情况。 Is windows closing it's internal handle after certain time and can this be forced to close immediately?. windows 是否在一定时间后关闭其内部手柄,是否可以立即强制关闭?

Depending on what behavior you want;取决于你想要什么行为; you can either put it in a UPS as 0A0D suggested;您可以按照 0A0D 的建议将其放入 UPS; but in addition you can use Windows' Vista+ Transactional NTFS functionality.但除此之外,您还可以使用 Windows 的 Vista+ Transactional NTFS功能。 This allows you to atomically write to the file system.这允许您以原子方式写入文件系统。 So in your case;所以在你的情况下; nothing would be written rather than improper data.什么都不会写,而不是不正确的数据。 It isn't directly part of the .NET Framework yet;它还不是 .NET 框架的直接一部分; but there are plenty of managed wrappers to be found online.但是网上有很多托管包装器。

Sometimes no data is better than wrong data.有时没有数据比错误的数据好。 When your application starts up again;当您的应用程序再次启动时; it can see that the file is missing;可以看到文件丢失了; it can "continue" from where it left off;它可以从中断的地方“继续”; depending on what your application does.取决于你的应用程序做什么。

Based on your comments, there is no guarantees when writing a file - especially if you lose power during a file write.根据您的评论,写入文件时无法保证 - 特别是如果您在文件写入期间断电。 Your best bet is to put the PC on an Uninterruptable Power Supply.最好的办法是将 PC 置于不间断电源上。 If you are able to create an auto-restore mechanism, like Microsoft Office products, then that would prevent complete loss of data but it won't fix the missing data upon power loss.如果您能够创建自动恢复机制,例如 Microsoft Office 产品,那么这将防止数据完全丢失,但不会在断电时修复丢失的数据。

I would consider this a case of a fatal exception (sudden loss of power).我认为这是一个致命的例外情况(突然断电)。 There isn't anything you can do about it, and generally, trying to handle them only makes matters worse.您对此无能为力,通常,试图处理它们只会让事情变得更糟。

I have had to deal with something similar;我不得不处理类似的事情; essentially an embedded system running on Windows, where the expectation is that the power might be shut off at any time.本质上是一个在 Windows 上运行的嵌入式系统,预期电源可能随时关闭。

In practice, I work with the understanding that a file written to disk less than 10 seconds before loss-of-power means that the file will be corrupted.在实践中,我的理解是,在断电前不到 10 秒写入磁盘的文件意味着该文件将被损坏。 (I use 30 seconds in my code to play it safe). (我在我的代码中使用 30 秒来保证安全)。

I am not aware of any way of guaranteeing from code that a file has been fully closed, flushed to disk, and that the disk hardware has finalized its writes.我不知道有任何方法可以从代码中保证文件已完全关闭,刷新到磁盘,并且磁盘硬件已完成写入。 Except to know that 10 (or 30) seconds has elapsed.除非知道 10(或 30)秒已经过去。 It's not a very satisfying situation, but there it is.这不是一个非常令人满意的情况,但确实如此。

Here are some pointers I have used in a real-life embedded project...以下是我在实际嵌入式项目中使用的一些指针...

  • Use a system of checksums and backup files.使用校验和和备份文件系统。
  • Checksums: at the end of any file you write, include a checksum (if it's a custom XML file then perhaps include a <checksum.../> tag of some sort).校验和:在您编写的任何文件的末尾,包含校验和(如果它是自定义 XML 文件,则可能包含某种<checksum.../>标记)。 Then upon reading, if the checksum tag isn't there, or doesn't match the data, then you must reject the file as corrupt.然后在阅读时,如果校验和标签不存在,或者与数据不匹配,那么您必须拒绝该文件为损坏。
  • Backups: every time you write a file, save a copy to one of two backups;备份:每次写入文件时,将副本保存到两个备份之一; say A and B. If A exists on disk but is less than 30 seconds old, then copy to B instead.说 A 和 B。如果 A 存在于磁盘上但不到 30 秒,则改为复制到 B。 Then upon reading, read the original file first.然后在阅读时,首先阅读原始文件。 If corrupt, then read A, if corrupt then read B.如果损坏,则读取 A,如果损坏,则读取 B。

Also

  • If it is an embedded system, you need to run the DOS command "chkdsk /F" on the drive you do your writes to, upon boot.如果它是嵌入式系统,则需要在启动时在写入的驱动器上运行 DOS 命令“chkdsk /F”。 Because if you are getting corrupted files, then you are also going to be getting a corrupted file system.因为如果你得到损坏的文件,那么你也将得到一个损坏的文件系统。
  • NTFS disk systems are meant to be more robust against errors than FAT32. NTFS 磁盘系统旨在比 FAT32 更强大地抵抗错误。 But I believe that NTFS disks can also require more time to fully flush their data.但我相信 NTFS 磁盘也可能需要更多时间来完全刷新数据。 I use FAT32 when I can.我尽可能使用 FAT32。

Final thought: if you are really using an embedded system, under windows, you would do well to learn more about Windows Embedded, and the Enhanced Write Filter system.最后的想法:如果您真的使用嵌入式系统,在 windows 下,您最好了解更多关于 Windows Embedded 和增强型写入过滤器系统的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM