public static byte[] MemoryToBytes(string str)
{
using (var memStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memStream))
{
streamWriter.WriteLine(str);
streamWriter.Flush();
return memStream.ToArray();
}
}