在C#和Windows事务文件的写作? [英] Transactional file writing in C# and Windows?

查看:226
本文介绍了在C#和Windows事务文件的写作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件,并从我不时需要编写一个更改文件。变化在于多个地方不断变化的信息。例如,改变一些数据接近文件的结尾,也改变起始附近的一些信息。我想两个独立的写操作要么都成功,要么都失败,否则留在不确定状态,有效地破坏。是否有.NET或一般为这种情况的内置支持?

I have a data file and from time to time I need to write a change to the file. The change consists of changing information in more than one place. For example, changing some data near the end of the file and also changing some information near the start. I want the two separate writes to either both succeed or both fail, otherwise it is left in uncertain state and effectively corrupted. Is there any builtin support for this scenario in .NET or in general?

如果没有,那么如何解决别人这个问题?如何在Windows上的数据库解决这个问题。

If not then how to others solve this issue? How does a database on Windows solve this issue?

更新:我不想使用事务性NTFS能力,因为它是不可用旧版Windows,如XP,它是在上述的文件覆盖场景缓慢。

UPDATE: I do not want to use the Transactional NTFS capability because it is not available on older version of Windows such as XP and it is slow in the file overwrite scenario as described above.

推荐答案

如果您使用的是Windows 6或更高版本(Vista中/ 7/2008 / 2008R2)的NTFS文件系统支持事务(包括分布式事务中),但你需要使用的P / Invoke调用Win32 API的(见本的question )。

If you are using Windows 6 or later (Vista/7/2008/2008R2) the NTFS filesystem supports transactions (including within a distributed transaction): but you will need to use P/Invoke to call Win32 APIs (see this question).

如果您需要在旧版本的Windows,或者非NTFS分区,则需要进行交易自己运行。这是断然不平凡:快满ACID功能,同时在工艺处理多个进程(包括通过共享远程访问)和系统崩溃甚至只有您的访问方法将要使用的假设(使用普通的Win32 API的其他进程当然会。打破东西)

If you need to run on older versions of Windows, or non-NTFS partitions you would need to perform the transactions yourself. This is decidedly non-trivial: getting full ACID functionality while handling multiple processes (including remote access via shares) across process and system crashes even with the assumption that only your access methods will be used (some other process using normal Win32 APIs would of course break things).

在这种情况下,数据库将几乎肯定会更简单:有一些过程中的数据库(SQL精简版,SQL精简版,...的),这样的数据库不需要服务器进程

In this case a database will almost certainly be easier: there are a number of in-process databases (SQL Compact Edition, SQL Lite, ...) so a database doesn't require a server process.

这篇关于在C#和Windows事务文件的写作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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