VB.Net | How do you get a string from a MemoryStream?

Here, we are going to learn how do you get a string from a MemoryStream in VB.Net?
Submitted by Nidhi, on December 23, 2020 [Last updated : March 08, 2023]

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.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.