保存到文件时如何确保数据不会损坏? [英] How to ensure that data doesn't get corrupted when saving to file?

查看:26
本文介绍了保存到文件时如何确保数据不会损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# 比较陌生,所以请多多包涵.

I am relatively new to C# so please bear with me.

我正在编写一个需要可靠的业务应用程序(在 C#、.NET 4 中).数据将存储在文件中.文件将定期修改(重写),因此我担心在保存数据时可能会出现问题(断电、应用程序被杀死、系统冻结……),而这会(我认为)导致文件损坏.我知道未保存的数据会丢失,但我不能丢失已保存的数据(因为损坏或...).

I am writing a business application (in C#, .NET 4) that needs to be reliable. Data will be stored in files. Files will be modified (rewritten) regularly, thus I am afraid that something could go wrong (power loss, application gets killed, system freezes, ...) while saving data which would (I think) result in a corrupted file. I know that data which wasn't saved is lost, but I must not lose data which was already saved (because of corruption or ...).

我的想法是每个文件都有 2 个版本,并且每次都重写最旧的文件.然后,如果我的应用程序意外结束,至少一个文件应该仍然有效.

My idea is to have 2 versions of every file and each time rewrite the oldest file. Then in case of unexpected end of my application at least one file should still be valid.

这是一个好方法吗?还有什么我可以做的吗?(数据库不是一个选项)

Is this a good approach? Is there anything else I could do? (Database is not an option)

感谢您的时间和回答.

推荐答案

很多程序使用这种方法,但通常他们会做更多的副本,以避免人为错误.

A lot of programs uses this approach, but usually, they do more copies, to avoid also human error.

例如,Cadsoft Eagle(一种用于设计电路和印刷电路板的程序)对同一文件最多可备份 9 个副本,称为 file.b#1 ... file.b#9

For example, Cadsoft Eagle (a program used to design circuits and printed circuit boards) do up to 9 backup copies of the same file, calling them file.b#1 ... file.b#9

您可以做的另一件事来加强安全性是散列:在文件末尾附加一个像 CRC32 或 MD5 这样的散列.当您打开它时,您检查 CRC 或 MD5,如果它们不匹配,则文件已损坏.这也将强制您避免意外或故意尝试使用其他程序修改您的文件的人.这也可以让您了解硬盘驱动器或 USB 磁盘是否损坏.

Another thing you can do to enforce security is to hashing: append an hash like a CRC32 or MD5 at the end of the file. When you open it you check the CRC or MD5, if they don't match the file is corrupted. This will also enforce you from people that accidentally or by purpose try to modify your file with another program. This will also give you a way to know if hard drive or usb disk got corrupted.

当然,保存文件操作越快,丢失数据的风险就越小,但你不能确定在写入期间或之后不会发生任何事情.

Of course, faster the save file operation is, the less risk of loosing data you have, but you cannot be sure that nothing will happen during or after writing.

考虑到硬盘驱动器、USB 驱动器和 Windows 操作系统都使用缓存,这意味着,如果您完成写入数据,可能是操作系统或磁盘本身仍未将其物理写入磁盘.

Consider that both hard drives, usb drives and windows OS uses cache, and it means, also if you finish writing the data may be OS or disk itself still didn't physically wrote it to the disk.

你可以做的另一件事,保存到一个临时文件,如果一切正常,你将文件移动到真正的目标文件夹中,这将减少半文件的风险.

Another thing you can do, save to a temporary file, if everything is ok you move the file in the real destination folder, this will reduce the risk of having half-files.

您可以将所有这些技术混合在一起.

You can mix all these techniques together.

这篇关于保存到文件时如何确保数据不会损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆