Parsi Coders
Run in memory - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: Visual Basic Programming (http://parsicoders.com/forumdisplay.php?fid=39)
+---- انجمن: Visual Basic.Net (http://parsicoders.com/forumdisplay.php?fid=43)
+---- موضوع: Run in memory (/showthread.php?tid=1292)



Run in memory - Amin_Mansouri - 11-27-2011

کد:
' open a file stream for reading, and load into a memory stream
  Public Shared Function StreamToMemory(ByVal path As String) As IO.MemoryStream
    Dim input As IO.FileStream
    Dim output As IO.MemoryStream
    input = New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    output = StreamToMemory(input)
    input.Close()
    Return output
  End Function
  ' transfer contents of input stream to memory stream
  Public Shared Function StreamToMemory(ByVal input As IO.Stream) As IO.MemoryStream
    Dim buffer(1023) As Byte
    Dim count As Integer = 1024
    Dim output As IO.MemoryStream
    ' build a new stream
    If input.CanSeek Then
      output = New IO.MemoryStream(input.Length)
    Else
      output = New IO.MemoryStream
    End If
    ' iterate stream and transfer to memory stream
    Do
      count = input.Read(buffer, 0, count)
      If count = 0 Then Exit Do
      output.Write(buffer, 0, count)
    Loop
    ' rewind stream
    output.Position = 0
    ' pass back
    Return output
  End Function