使用C#读取(使用Filesystem.FileGet)VB6记录文件(使用Put编写) [英] Reading (with Filesystem.FileGet) VB6 record file (written with Put) with C#

查看:81
本文介绍了使用C#读取(使用Filesystem.FileGet)VB6记录文件(使用Put编写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Visual Basic 6产生的旧版数据库文件中读取数据.从旧版软件中,我发现该文件是使用 Put 编写的,并将某种记录作为参数传递给 Put 函数.这些结构定义如下:

I need to read data from a legacy database file produced by Visual Basic 6. From the legacy software I found that file was written using Put and passing sort of records as parameters to the Put function. These structures are defined as follows:

Type THE_TYPE
    FIELD_1 As Single
    FIELD_2 As String * 20
    FIELD_3(1 To 50) As Single
    FIELD_4(1 To 10) As String * 1
End Type

我的类型更大,更复杂,但是我在项目中使用了不同的定义,即 THE_TYPE .我发现导入 Microsoft.VisualBasic 使我可以访问VB函数,类似于用于写入文件的函数,因此我要使用 FileSystem.OpenFile()<打开和关闭文件 .CloseFile(); 现在,我最终需要读取其中包含的数据,因为原始功能是:

My Types are larger and more complex but I've put in THE_TYPE the different definitions I have in my project. I've found that importing Microsoft.VisualBasic gives me access to VB functions similar to those used to write the file, so I'm opening and closing the file with FileSystem.OpenFile() and .CloseFile(); now I need to finally read data contained and since the original function was:

Public RecordContent As THE_TYPE
[...] 
Get #1, recordNumber, RecordContent 

我想我可以使用类似的东西,例如 Microsoft.VisualBasic.FileSystem.FileGet().
所以问题是,我想如何定义一个类似于原始VB6 Type THE_TYPE 的容器?如何调用 .FileGet()正确填充此对象?

I suppose I can use something similar, like Microsoft.VisualBasic.FileSystem.FileGet().
So the question is, how do I define a container, I suppose a class, similar to the original VB6 Type THE_TYPE? How do I call .FileGet() to correctly fill this object?

推荐答案

关键是在VB.NET中的结构声明上正确定义属性.如果结构始终由 FileGet 初始化,则无需在构造函数中手动初始化其固定字段,否则,您可以这样做.

The key is defining attributes properly on the structure declaration in VB.NET. Provided the structure will always be initialized by FileGet, you don't need to manually initialize its fixed fields in a constructor, otherwise, you do.

Structure THE_TYPE
    Public FIELD_1 As Single
    <VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=20)> Public FIELD_2 As String
    <VBFixedArray(49)> Public FIELD_3 As Single()
    <VBFixedArray(9)> Public FIELD_4 As Char()
End Structure

很显然,数组必须从零开始,因此上限向下移动.

Obviously, the arrays will have to start from zero, so the upper bounds are shifted down.

从文件读取:

Dim d As System.ValueType = New THE_TYPE()

FileOpen(1, "...", OpenMode.Random, OpenAccess.Read, OpenShare.Default, 234)
FileGet(1, d, 1)
FileClose(1)

234 是VB6中结构的大小.在VB.NET中更大,因此您想对其进行硬编码.

234 is the size of the structure in VB6. It's bigger in VB.NET, so you want to hardcode that.

这篇关于使用C#读取(使用Filesystem.FileGet)VB6记录文件(使用Put编写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆