var contents = "Hello world"; var ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(contents));
var fi = new FileInfo(sourceFile); using (var fs = fi.OpenRead()) { using (var ms = new MemoryStream()) { ms.SetLength(fs.Length); fs.Read(ms.GetBuffer(), 0, (int)fs.Length); ms.Flush(); fs.Close(); // Do something here with the memory stream } }