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

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:

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")

        '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: ")
    End Sub
End Module


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

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.