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

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

问题描述

我是比较新的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公司鹰(程序用于设计电路和印刷电路板)做了相同的文件的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,如果他们不匹配的文件已损坏。
这也会从人们意外地或目的,试图修改您的文件与另一个程序强制你。
这也给你一个办法知道,如果硬盘或U盘被损坏了。

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天全站免登陆