Transactional file writing in C# and Windows?

前端 未结 2 1204
天命终不由人
天命终不由人 2020-12-20 12:31

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

相关标签:
2条回答
  • 2020-12-20 13:25

    DB basically uses a Journal concept (at least those one I'm aware of). An idea is, that a write operation is written in journal until Writer doesn't commit a transaction. (Sure it's just basic description, it's so easy)

    In your case, it could be a copy of your file, where you're going to write a data, and if everything finished with success, substitute original file with it's copy.

    Substitution is: rename original file like a old, rename backup file like a original.

    If substitution fails: this is a critical error, that application should handle via fault tolerance strategies. Could be that it informed a user about a failed save operation, and tries to recover. By the way in any moment you have both copies of your file. That one when write operation just started, and that one when write operation finished.

    This techniques we used on past projects on VS IDE like systems for industrial control with pretty good success.

    0 讨论(0)
  • 2020-12-20 13:30

    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).

    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).

    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.

    0 讨论(0)
提交回复
热议问题