Results 1 to 5 of 5

Thread: [2005] MemoryStream TextWriter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [2005] MemoryStream TextWriter

    Hi Guys,

    Just having a small issue with a MemoryStream, I think my only real issue is lack of education.

    I am wanting to dump a MemoryStream to a text file (.csv). Now I have some code here that does this but I'm left thinking there is a better or more elegant way to do this.

    VB Code:
    1. Private Sub StreamToFile(ByVal sFilePath As String, ByVal objStream As System.IO.MemoryStream)
    2.  
    3.         Dim objTxtWriter As System.IO.TextWriter = Nothing
    4.         Dim objReader As System.IO.StreamReader = Nothing
    5.  
    6.         Try
    7.             objTxtWriter = New System.IO.StreamWriter(sFilePath)
    8.  
    9.             objReader = New System.IO.StreamReader(objStream)
    10.  
    11.             objTxtWriter.Write(objReader.ReadToEnd())
    12.  
    13.             objReader.Close()
    14.             objTxtWriter.Close()
    15.  
    16.         Catch ex As Exception
    17.             MessageBox.Show(ex.Message)
    18.         Finally
    19.             If objTxtWriter IsNot Nothing Then
    20.                 objTxtWriter.Dispose()
    21.                 objTxtWriter = Nothing
    22.             End If
    23.  
    24.             objReader.Dispose()
    25.         End Try

    As you can see I am using a StreamReader to get the contents of the stream as a string and dump it. My gut feeling is that I should be able to dump this stream without using a reader?

    Is the above method using a reader the only way? Can I feed a raw MemoryStream into a file??

    I have done many searches but found only unlcear or incomplete answers.

    Thanks PPL,
    Matt.

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: [2005] MemoryStream TextWriter

    Try the WriteTo method of the MemoryStream. The help says:
    Writes the entire contents of this memory stream to another stream.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] MemoryStream TextWriter

    Using Mr.No's suggestion:
    VB Code:
    1. Private Sub StreamToFile1(ByVal sFilePath As String, ByVal objStream As System.IO.MemoryStream)
    2.     Using sr As New IO.StreamWriter(sFilePath)
    3.         objStream.WriteTo(sr.BaseStream)
    4.     End Using
    5. End Sub
    or another way:
    VB Code:
    1. Private Sub StreamToFile2(ByVal sFilePath As String, ByVal objStream As System.IO.MemoryStream)
    2.     Dim count As Integer = Convert.ToInt32(objStream.Length)
    3.     Dim buffer(count - 1) As Byte
    4.  
    5.     objStream.Read(buffer, 0, count)
    6.     IO.File.WriteAllBytes(sFilePath, buffer)
    7. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] MemoryStream TextWriter

    PEEEERRRRRRFECT!!!!!

    That's exactly the sort of answer I was chasing. I knew there had to be a more elegant solution!

    Thanks to the both of you, I had seen the WriteTo method but hadn't made the metal 'skip'.

    Love your work JM!!!!! Love it!

    Again thank you to you both...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] MemoryStream TextWriter

    You could just use a FileStream and forget the StreamWriter too. In that case:
    VB Code:
    1. Using sr As New IO.StreamWriter(sFilePath)
    2.         objStream.WriteTo(sr.BaseStream)
    3.     End Using
    would become:
    VB Code:
    1. Using fs As New IO.FileStream(sFilePath, IO.FileMode.Create)
    2.         objStream.WriteTo(fs)
    3.     End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width