Using a MemoryStream • The most common use of a MemoryStream is - TopicsExpress



          

Using a MemoryStream • The most common use of a MemoryStream is to store temporarily data that will be written to a file eventually. • Using a MemoryStream, you can take your time to create the stream in memory, add data to it, and then write it all to disk at once—minimizing the time the file needs to be locked open. • In multi-user environments where a file might need to be accessed by other processes, this minimizes the potential for conflict. using System; using System.IO; class Program { static void Main(string[] args) { // Create a MemoryStream object MemoryStream ms = new MemoryStream(); // Create a StreamWriter object to allow // writing strings to the MemoryStream StreamWriter sw = new StreamWriter(ms); // Write to the StreamWriter and MemoryStream sw.WriteLine("Hello, World!"); // Flush the contents of the StreamWriter so it can be written to disk sw.Flush(); // Write the contents of the MemoryStream to a file ms.WriteTo(File.Create("c:\test\memory.txt")); // Close the file and MemoryStream sw.Close(); ms.Close(); } }
Posted on: Thu, 10 Oct 2013 11:39:26 +0000

Trending Topics



dy" style="min-height:30px;">
The Levites - Should We Retire at Fifty? By :: Bienhome

Recently Viewed Topics




© 2015