I have some code in C# where I am trying to write to a MemoryStream via a StringWriter example code:
using (MemoryStream stream = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter())
{
writer.WriteLine("SELECT");
writer.WriteLine("\tT.*");
writer.WriteLine("FROM");
writer.WriteLine("\t[A Table] T");
writer.WriteLine("WHERE");
writer.WriteLine($"\tT.[Data Field] IN ({data})");
}
....
return new MemoryStream(stream.GetBuffer());
}
When I inspect the contents of stream
I see that the .NET Framework has added extra 0
bytes to the end of the buffer to take it up to 256 bytes. I can not find anything in the MSDN help and it is puzzling.
To work around this (using the above example) I did the following:
string crlf = Environment.NewLine;
string value = $"SELECT{crlf}";
value += $"\tT.*{crlf}";
value += $"FROM{crlf}";
value += $"\t[A Table] T{crlf}";
value += $"WHERE{crlf}";
value += $"\tT.[Data Field] IN ({data})";
return new MemoryStream(Encoding.UTF8.GetBytes(value));
I would rather use a stream writer than to build up a string and then call Encoding.UTF8.GetBytes(value)
I have tried using a StringBuilder as an intermediary step but it still adds the extra characters to the end.