So I'm using a streamwriter which I feed a stream, not a path. I'm not able to use a path directly, because it must take into account a mocked filesystem.
The problem with this, is that the append bool is only an option if you provide a path, not a stream.
If I do this:
using (var writer = new StreamWriter(stream, encoding))
then I can't set it to append instead of overwrite.
I've also tried to set it to the .AppendText()
stream of the file, like this:
using (var writer = FileInfo.FromFileName(path).AppendText())
This works, but the problem here is that I can't set the encoding, which I need to do. I've not found a way to set the encoding after it's been constructed.
So in essence, I need a way to be able to use a streamwriter to append text, while also taking into account that I have to feed it a stream, as well as an encoding. Anyone have an idea about what I can do?
Just set the position of stream to the end before you create the StreamWriter.
stream.Position = stream.Length;