locked
Converting a Memorystream into a filestream RRS feed

  • Question

  • I'm working on a project to extract documents from a SQL Database and writing those documents to a network drive. However some of the files pointed at in the DB are encrypted files (RijndaelManaged) that reside on another network drive.  I have been able to retrieve, and decrypt the files using streams. Now I’m having problems saving or converting the Memorystream into an actual file.

     

    Here is my code

    Public Sub ExtractDocument(ByVal ThisDocumentInfo As DBFileInfo, ThisMS As System.IO.MemoryStream)

            REM ThisMS is the MemoryStream holding the Decrypted Document to be saved

     

            Dim sFileName_wk As String = QQ

            Dim sExtractFile As String = QQ

     

            Dim sBaseFile As String = QQ

            Dim sThisFile As String = QQ

            Dim sSQL As String = QQ

            Dim bPassed As Boolean = True

            Dim TryCount As Integer = 0

            Dim code_type_wk As Long = 0

            Dim img As Object = Nothing

            Dim sThisFileInfo As FileInfo

            Dim sFileStream As FileStream

     

            REM Construct the file name and path to ve saved

            With ThisDocumentInfo

                sBaseFile = .FolderIndex & "_" & .Rec_Id    'Filename

                sExtractFile = sBaseFile & "." & .oFileType 'Filename with Document Type

                frmMain.txtFoldexIdx.Text = CStr(.FolderIndex)

            End With

     

            Debug.Print(sBaseFile)

            sThisFile = sEncryptPath & sExtractFile    'Path and Filname - Location to write the file

            sThisFileInfo = New FileInfo(sThisFile)

     

            sThisFileInfo.Refresh()

     

            REM Remove Previous version

            If sThisFileInfo.Exists Then

                sThisFileInfo.Delete()

            End If

     

            sThisFileInfo.Refresh()

          

            REM Write the Decrypted file to it's New Location

            sFileStream = New FileStream(sThisFile, FileMode.Create)

            Dim sStreamWriter As New StreamWriter(sFileStream)

            sStreamWriter.Write(ThisMS)

            sStreamWriter.Close()

            sFileStream.Close()

     

            REM check to see if it worked

            If sThisFileInfo.Exists Then

                Stop  'GO

            Else

                Stop  'NO GO

            End If

     

     

        End Sub

     

     


    VC Swindell PepperEyes.com
    Tuesday, July 5, 2011 4:31 PM

Answers

  • here's how to write a memorystream to a file:

     

    Dim outStream As IO.FileStream = IO.File.OpenWrite("FileName")
    ms.WriteTo(outStream)
    outStream.Flush()
    outStream.Close()
    

     


    thanks for any help
    Tuesday, July 5, 2011 4:36 PM
  • Hi,

    And when you try the problem is ? Please be always explicit about your issue.

    A common issue is forgetting to reset the memory stream at position 0 so that reads start from the beginning once you have written to it.

    Thanks to Paul's response I finally spotted the "sStreamWriter.Write(ThisMS)" line that shouldn't even compile ? If this is the case please tell explicitely  that the code you show us doesn't compile and on which line it happens.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".




    Tuesday, July 5, 2011 6:05 PM

All replies

  • here's how to write a memorystream to a file:

     

    Dim outStream As IO.FileStream = IO.File.OpenWrite("FileName")
    ms.WriteTo(outStream)
    outStream.Flush()
    outStream.Close()
    

     


    thanks for any help
    Tuesday, July 5, 2011 4:36 PM
  • Hi,

    And when you try the problem is ? Please be always explicit about your issue.

    A common issue is forgetting to reset the memory stream at position 0 so that reads start from the beginning once you have written to it.

    Thanks to Paul's response I finally spotted the "sStreamWriter.Write(ThisMS)" line that shouldn't even compile ? If this is the case please tell explicitely  that the code you show us doesn't compile and on which line it happens.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".




    Tuesday, July 5, 2011 6:05 PM