Search code examples
vb.netvb6binaryreader

VB 6.0 Binary Reading and Writing from VB.NET


I have a vb.net code and want to convert it in vb 6.0. But I have some difficulties. I cant find equivalent of some .net classes

            Dim byteswritten As Integer
            Dim fs As System.IO.FileStream
            Dim r As System.IO.BinaryReader
            Dim CHUNK_SIZE As Integer = 65554
            fs = New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            r = New System.IO.BinaryReader(fs)
            Dim FSize As Integer = CType(fs.Length, Integer)
            Dim chunk() As Byte = r.ReadBytes(CHUNK_SIZE)

            While (chunk.Length > 0)
                dmPutStream.Write(chunk, chunk.Length, byteswritten)
                If (FSize < CHUNK_SIZE) Then
                    CHUNK_SIZE = FSize
                    chunk = r.ReadBytes(CHUNK_SIZE)
                Else
                    chunk = r.ReadBytes(CHUNK_SIZE)
                End If
             End While

Well, the document can be big then we used chunk. But I dont know steps for vb 6.0

Such as what i should do for binary reading.


Solution

  • Without all your code for opening the write stream and closing the read and write streams, here's an example of how you can do it in VB6 using ADODB.Stream.

    Under Project | References, add a reference to ADO Active X Data Objects Library. My version is 6.1, but you should be okay to just choose the latest version - depends on what version of ADO is installed on your system

    Hope it helps - more info online if you want to look at all the ADODB.Stream methods and properties

    Public Sub StreamData(strWriteFilename As String, filePath As String)
    
        Const CHUNK_SIZE As Long = 65554
    
        Dim byteswritten    As Integer
        Dim FSize           As Long
    
        Dim adofs           As New ADODB.Stream 'Object 'System.IO.FileStream
        Dim varData         As Variant
    
        ' Include this here - but probably defined elsewhere
        Dim dmPutStream     As New ADODB.Stream
    
        ' Open Write Stream
        ' *** Looks like you do this elsewhere
        Set dmPutStream = CreateObject("ADODB.Stream")
        With dmPutStream
            .Type = adTypeBinary
            .Open strWriteFilename, adModeWrite
        End With
    
        ' Open Read strema and start pushing data from it to the write stream
        Set adofs = CreateObject("ADODB.Stream") 'New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        With adofs
            .Type = adTypeBinary
            .Open
            .LoadFromFile filePath
    
            ' Size of Read file - do you want this?
            FSize = .Size
    
            varData = .Read(CHUNK_SIZE)
            Do While Len(varData) > 0
                dmPutStream.Write varData
                If Not .EOS Then
                    varData = .Read(CHUNK_SIZE)
                End If
            Loop
    
            .Close
        End With
    
        'Save binary data To disk
        dmPutStream.SaveToFile strWriteFilename, adSaveCreateOverWrite
        dmPutStream.Close
    
    End Sub