I am trying to open a file in c#, then write some text into it, and conclude by dumping a compression of a bytes array into the same file. So, the file I want to create is something like
sometextinascii
somemoretextinascii
BUNCHOFNONHUMANREADABLEBYTES
and what I did was the following:
// Code where we get values of the following variables:
// string filename
// Byte[] buffer
using (FileStream file = new(filename, FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new(file))
{
writer.WriteLine("sometext");
writer.WriteLine("somemoretext");
}
using (GZipStream zipStream = new(file, CompressionMode.Compress))
{
zipStream.Write(buffer, 0, buffer.Length);
}
}
but this does not work. When I run this code, I get the error
System.ArgumentException: 'Stream does not support writing. (Parameter 'stream')'
when the variable zipStream
is being created.
What I don’t understand is that if I remove the StreamWriter
part of the code, then the code works fine. So, I have two questions:
- Does the streamwriter somehow close the
file
once it closes itself? - If yes, how can I prevent that? If no, what is the cause of my error?