首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

VB6.0对VB.NET二进制文件的读写

在 VB6.0 和 VB.NET 中读写二进制文件的方式有所不同。以下是如何在这两种环境中进行二进制文件读写的详细说明。

在 VB6.0 中读写二进制文件

在 VB6.0 中,可以使用 OpenGetPut 语句来读写二进制文件。

写二进制文件

代码语言:javascript
复制
Private Sub WriteBinaryFile(filePath As String, data() As Byte)
    Dim fileNum As Integer
    fileNum = FreeFile
    Open filePath For Binary Access Write As #fileNum
    Put #fileNum, , data
    Close #fileNum
End Sub

读二进制文件

代码语言:javascript
复制
Private Function ReadBinaryFile(filePath As String) As Byte()
    Dim fileNum As Integer
    Dim fileLength As Long
    Dim data() As Byte
    
    fileNum = FreeFile
    Open filePath For Binary Access Read As #fileNum
    fileLength = LOF(fileNum)
    ReDim data(fileLength - 1)
    Get #fileNum, , data
    Close #fileNum
    
    ReadBinaryFile = data
End Function

在 VB.NET 中读写二进制文件

在 VB.NET 中,可以使用 FileStreamBinaryReader/BinaryWriter 类来读写二进制文件。

写二进制文件

代码语言:javascript
复制
Imports System.IO

Private Sub WriteBinaryFile(filePath As String, data() As Byte)
    Using fs As New FileStream(filePath, FileMode.Create, FileAccess.Write)
        fs.Write(data, 0, data.Length)
    End Using
End Sub

读二进制文件

代码语言:javascript
复制
Imports System.IO

Private Function ReadBinaryFile(filePath As String) As Byte()
    Using fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
        Dim data(fs.Length - 1) As Byte
        fs.Read(data, 0, data.Length)
        Return data
    End Using
End Function
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券