Search code examples
c#filefile-iotransactionstransactional

Transactional Write of multiple FileStreams in C#


I'm supposed to get multiple files with the same extension (for example, an archive that's split into several archives: rar, r00, r01, etc).

I'm trying to find a solution where if one of the file streams I got fails to be written, all the previously successful files that were created will be deleted. Just like a transactional file stream writer.

I've bumped into .NET Transactional File Manager project, which seems just like what I need -- except it doesn't work with streams but with file paths.

At this point I only see two options:

  • Keeping a list of successful file writes and if other one will fail, I'll go over the list and delete all.
  • Write all files to %TEMP% or something via FileStream and then - after all files were written successfully, I'll use the Transactional File Manager (mentioned above) to move the files to the desired location.

Need to notice that I have to work with streams

Which of the two options is better in your opinion? Is there any better recommendation or idea for doing this?

Thanks


Edit:

Another option I bumped into is using AlphaFS just like in the following example. Any thoughts on this?


Solution

  • I ended up using AlphaFS. Using it just like in this example. It works perfectly and does exactly what I was needed.

    Thanks for all the comments.