ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

WriteToFileUtf8

Write File in UTF8 from MemoryStream

Source

public static void WriteToFileUtf8(this MemoryStream stream, String path)
{
    using (var writer = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
    {
        Encoding enc = new UTF8Encoding(false, false);
        var chars = enc.GetString(stream.ToArray());

        var bytes = enc.GetBytes(chars.ToCharArray());
        writer.Write(bytes, 0, bytes.Length);
    }
}

Example

var path = "C:\\doc.txt";

using (MemoryStream ms = new MemoryStream())

   ms..WriteToFileUtf8(path);
}

Author: Guilherme Oliveira

Submitted on: 27 sep 2019

Language: C#

Type: MemoryStream

Views: 185