VB 6.0 VB.NET二进制读写

时间:2016-07-14 13:26:59

标签: vb.net vb6 binaryreader

我有一个vb.net代码,想要在vb 6.0中进行转换。但我有一些困难。我找不到相当于一些.net类

            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

那么,文档可能很大,然后我们使用chunk。但我不知道vb 6.0的步骤

比如我应该为二进制阅读做些什么。

2 个答案:

答案 0 :(得分:1)

将VB.NET转换为VB6是一个坏主意,完全没必要。如果需要使用VB6应用程序中的VB.NET代码,最好的办法是为.NET库创建一个COM可见的包装器,并从VB6应用程序调用该包装器。

您可能可以使用VB6在功能上转换代码,但确实没有意义。 VB.NET是一种比VB6更好的语言,使用它的COM功能可以避免编写无穷无尽的粗略VB6代码。

如果您已完成此操作,则需要在功能上重现Stream和Reader类。

这是FileStream.cs的源代码: http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs

对于BinaryReader: http://referencesource.microsoft.com/#mscorlib/system/io/binaryreader.cs

答案 1 :(得分:1)

如果没有用于打开写入流和关闭读取和写入流的所有代码,以下是使用ADODB.Stream在VB6中执行此操作的示例。

Project | References下,添加对ADO Active X Data Objects Library的引用。我的版本是6.1,但您可以选择最新版本 - 取决于系统上安装的ADO版本

希望有所帮助 - 如果您想查看所有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