首页 > 解决方案 > 如何从 vb.net 中的二进制文件中读取 vb6 定长字符串

问题描述

语境

我有一个使用这种特定类型的vb6软件:

Type Example
    A As Integer
    B As Single
    C As String * 10
    D  As Byte
    E As String
End Type

这个结构完全保存在一个带有简单“ put ”的二进制文件中:

Dim Numfile%
Numfile = FreeFile
[...]
Put #Numfile, , example_instance
[...]

问题

现在我想从vb.net (.NET Framework 4) 读取这个二进制文件。问题是在.net中我们没有固定的字符串......我试图写一些类似的东西:

Structure Example
     Dim A As Short
     Dim B As Single 
     Dim C() As Char ' ---> This should replace String * 10.
     Dim D As Byte 
     Dim E As String 
End Structure

然后我阅读了文件:

Dim n as short = FreeFile()
Dim instance as Example
If FileExists(filename) Then
    FileOpen(n, filename, OpenMode.Binary, OpenAccess.Read)
    [...]        
    FileGet(n, instance)
    [...]

但是,很明显,原始数据没有被正确检测到。

如何在 vb.net 中正确读取此二进制文件?

标签: vb.netvb6vb6-migration

解决方案


我会写这个作为答案,即使我不能 100% 确定它会起作用,因为这不是我曾经做过的事情。该VBFixedString属性专门用于 VB6 将使用固定长度的情况String。因此,我认为像这样声明你的类型:

Structure Example
     Dim A As Short
     Dim B As Single
     <VBFixedString(10)> Dim C As String
     Dim D As Byte
     Dim E As String
End Structure

应该做的伎俩。


推荐阅读