Hi I have the following code which I have reduced down from a bigger task I am working on. Essentially though I have found that when combining memory streams together I am getting a special character on the join position. Below is the code for the app to run in full which shows the problem. Opening the resulting export.csv file in Visual Studio Code shows a special char at the start of the 3rd line. If you open the CSV in excel you will notice that the start of the 3rd line is a different appearance than the other lines.
using System;
using System.IO;
using System.Text;
namespace testingMemory
{
class Program
{
static void Main(string[] args)
{
var stream1 = GetMemoryStream("section1");
var stream2 = GetMemoryStream("section2");
var fileStream = new FileStream("export.csv", FileMode.Truncate, FileAccess.Write);
stream1.WriteTo(fileStream);
stream2.WriteTo(fileStream);
}
public static MemoryStream GetMemoryStream(string section)
{
var wrapper = "\"";
var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
streamWriter.WriteLine($"{wrapper}{section}{wrapper},{wrapper}1{wrapper}");
streamWriter.Flush();
streamWriter.WriteLine($"{wrapper}{section}{wrapper},{wrapper}2{wrapper}");
streamWriter.Flush();
return memoryStream;
}
}
}
Each of the streams contains a byte order mark. And when you copy both, the byte order mark from the first stream is used as BOM for the file and the second one is just garbage in the middle. Your memory streams should not contain BOMs:
using System;
using System.IO;
using System.Text;
namespace testingMemory
{
class Program
{
static void Main(string[] args)
{
using (var stream1 = GetMemoryStream("section1"))
using (var stream2 = GetMemoryStream("section2"))
{
using (var fileStream = new FileStream("d:\\export.csv", FileMode.Truncate, FileAccess.Write))
{
stream1.WriteTo(fileStream);
stream2.WriteTo(fileStream);
}
}
}
public static MemoryStream GetMemoryStream(string section)
{
var wrapper = "\"";
var memoryStream = new MemoryStream();
// Using a non-default UTF-8 encoding with BOM not used:
var streamWriter = new StreamWriter(memoryStream, new UTF8Encoding(false));
streamWriter.WriteLine($"{wrapper}{section}{wrapper},{wrapper}1{wrapper}");
streamWriter.Flush();
streamWriter.WriteLine($"{wrapper}{section}{wrapper},{wrapper}2{wrapper}");
streamWriter.Flush();
return memoryStream;
}
}
}