Home »
VB.Net »
VB.Net Q/A
VB.Net | How do you get a string from a MemoryStream?
By Nidhi Last Updated : November 15, 2024
MemoryStream Class
The MemoryStream is a predefined class in the .NET framework class library. It is used to read and write data into memory directly. Now we will look, how to read a string from MemoryStream in the VB.Net program.
VB.Net code to get a string from a MemoryStream
Imports System.IO
Module Module1
Sub Main()
'Create a memory stream
Dim memStream As New MemoryStream
Dim stream As New StreamWriter(memStream)
stream.WriteLine("Learn programming from includehelp.com")
stream.Flush()
'Now set the memory stream into the beginning.
memStream.Position = 0
Dim streamReader As New StreamReader(memStream)
Dim Str As String
'Here we read data from to string.
Str = streamReader.ReadToEnd()
Console.WriteLine("Data in memory stream: ")
Console.WriteLine(Str)
End Sub
End Module
Output
Learn programming from includehelp.com
In the above program, we write the data into the memory stream using MemoryStream class, and then we read data from the memory stream using StreamReader class and then print the string on the console screen.