Thursday, 26 April 2012

Beginning .Net , C# Tips : Write To a MemoryStream in C# Programming

There is a situation where you want to write data in Memory Stream.
You can write to the stream by encoding a string containing the information you want to write to a byte array and then using the stream’s Write method to write the byte array to the MemoryStreams.
The Close method also calls Flush internally to commit the data to the data store.

Here are sample Example
C# Example :
    byte[] data = System.Text.Encoding.ASCII.GetBytes("This is a sample string");
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    ms.Write(data, 0, data.Length);
    ms.Close();

VB.net Example :
    Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes("This is a sample string")
    Dim ms As New System.IO.MemoryStream()
    ms.Write(data, 0, data.Length)
    ms.Close()

No comments:

Post a Comment