Stream - ToByteArray
A Stream extension method that converts the Stream to a byte array.
public static string FileName = "test.txt"; public static void Main() { SaveFile(); byte[] bytes; using (StreamReader sr = File.OpenText(FileName)) { // C# Extension Method: Stream - ToByteArray bytes = sr.BaseStream.ToByteArray(); } var file = FileName.ToFileInfo(); // C# Extension Method: FileInfo - WriteAllBytes file.WriteAllBytes(bytes); Console.WriteLine(file.ReadAllText()); } private static void SaveFile() { var html = @"<!DOCTYPE html> <html> <body> <h1>This is <b>bold</b> heading</h1> <p>This is <u>underlined</u> paragraph</p> <h2>This is <i>italic</i> heading</h2> </body> </html> "; html.SaveAs(FileName); }
View Source
using System.IO; public static partial class Extensions { /// <summary> /// A Stream extension method that converts the Stream to a byte array. /// </summary> /// <param name="this">The Stream to act on.</param> /// <returns>The Stream as a byte[].</returns> public static byte[] ToByteArray(this Stream @this) { using (var ms = new MemoryStream()) { @this.CopyTo(ms); return ms.ToArray(); } } }