VB 6.0 二进制读写 来自 VB.NET

VB 6.0 Binary Reading and Writing from VB.NET

我有一个 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

嗯,文件可以大,然后我们使用块。但是我不知道 vb 6.0

的步骤

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

将 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

对于二进制Reader: http://referencesource.microsoft.com/#mscorlib/system/io/binaryreader.cs

如果没有用于打开写入流和关闭读取和写入流的所有代码,下面是一个示例,说明如何使用 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