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.