简体   繁体   English

VB6至VB.NET写入随机访问文件

[英]VB6 to VB.NET write to random access file

I have a legacy VB6 application that writes to a file in random access mode. 我有一个旧版VB6应用程序,它以随机访问模式写入文件。 This file is then read by an application developed by third party. 然后由第三方开发的应用程序读取此文件。

I have been tasked with rewriting the VB6 app in VB.NET however the third party application will not change. 我的任务是在VB.NET中重写VB6应用程序,但是第三方应用程序不会更改。 I have attempted to convert the VB6 code to VB.NET however the random access file is not being read correctly. 我试图将VB6代码转换为VB.NET,但是随机访问文件未正确读取。

Provided below are condensed snippets of both the VB6 and VB.NET code. 下面提供的是VB6和VB.NET代码的压缩摘要。 The VB.NET code is successfully writing to the file however, the field lengths are not correct and the application reading the file is not parsing the data correctly. VB.NET代码已成功写入文件,但是,字段长度不正确,并且读取文件的应用程序未正确解析数据。 How can I go about writing to the random access file in the same manner? 如何以相同的方式写入随机访问文件?

I have searched around but have not found a solution that works. 我到处搜索,但是找不到有效的解决方案。

VB6 VB6

Type Person
    ID as String * 5
    Name as String * 25
    EyeColor as String * 10
End Type

Dim myPerson as Person
myPerson.ID = "13"
myPerson.Name = "Joe"
myPerson.EyeColor = "Blue"

Open <file path> For Random As <file number> Len = Len(myPerson)
    Put <file number>, myPerson.ID, myPerson
Close <file number>

VB.NET VB.NET

Structure Person
    <VBFixedString(5)> Dim ID As String
    <VBFixedString(25)> Dim Name As String
    <VBFixedString(10)> Dim EyeColor As String
End Structure

Dim myPerson as New Person
myPerson.ID = "13"
myPerson.Name = "Joe"
myPerson.EyeColor = "Blue"

FileOpen(<file number>, <file path>, OpenMode.Random, , , Len(myPerson))
    FilePut(<file number>, myPerson, myPerson.ID)
FileClose(<file number>)

Not sure if this solves the encoding problems.... 不确定是否可以解决编码问题。

I hardcoded the FilePut RecordNumber in the test. 我在测试中对FilePut RecordNumber进行了硬编码。

Structure Person
    <VBFixedString(5)> Dim ID As String
    <VBFixedString(25)> Dim Name As String
    <VBFixedString(10)> Dim EyeColor As String
End Structure

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim myPerson As New Person
    myPerson.ID = "13".PadLeft(5)
    myPerson.Name = "Joe".PadLeft(15)
    myPerson.EyeColor = "Blue".PadLeft(10)
    Stop

    FileOpen(1, "C:\temp\random.bin", OpenMode.Random, , , Len(myPerson))
    FilePut(1, myPerson, 2) ' needs integer RecordNumber - depreciated?
    FileClose(1)
End Sub

Visual Studio has a hex editor - I think is an option under File, Open Visual Studio有一个十六进制编辑器-我认为这是“文件”,“打开”下的一个选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM