使用C#打开旧的VB6随机访问文件

问题描述 投票:1回答:1

我正在尝试打开使用随机访问创建的旧VB6文件。

使用的类型如下:

Type CDB
    dateCreated As Date
    lastModified As Date
    companyName As String * 30
    ownerName As String * 30
    contactName As String * 30
    addresss As String * 100
    tel As String * 75
    vat As String * 8
    BRegd As String * 9
End Type

访问如下:

Dim CDB As CDB
Open "CLIENTS.DAT" For Random As #1 Len = Len(CDB)
Lastrec = LOF(1) / Len(CDB)

For rec = 1 To Lastrec
    Get #1, rec, CDB
    txtDateCreated.Text = Format(CDB.dateCreated, "dd/mm/yyyy")
    txtLastModified.Text = Format(CDB.lastModified, "dd/mm/yyyy")
    txtCompanyName.Text = Trim(CDB.companyName)
    ... and so on
Next

现在,我想使用C#打开此文件,并导入SQL数据表中的所有数据。任何人都可以帮我使用Type CDB作为结构打开这个文件吗?

c# vb6 randomaccessfile
1个回答
2
投票

要使用我的示例,您需要为Microsoft.VisualBasic.Filesystem程序集创建别名:

Imports VB6FileSystem = Microsoft.VisualBasic.FileSystem
Imports VB = Microsoft.VisualBasic

然后在你的代码中:

// make sure the read buffer is big enough
string testReadData = "".PadRight(128);
int filenumber = VB6FileSystem.FreeFile();
VB6FileSystem.FileOpen(filenumber, @"c:\temp\test.dat", VB.OpenMode.Random,  RecordLength: 128);

// Write some test data ....
VB6FileSystem.FilePut(filenumber, "Testdaten 1", 1, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 4", 4, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 14", 14, true);
// Read some data ...
VB6FileSystem.FileGet(filenumber, ref testReadData, 14, true);
VB6FileSystem.FileClose(filenumber);

当然你必须分析旧的记录结构(vb6知道“固定长度字符串”,而C#并不真正了解它们......)以及数据如何在文件中表示。也许你应该在c#中读取一个字节数组来处理二进制数据(比如日期,数字......)“手工”。

我没有尝试的是使用字节数组引用变量的FileGetObject方法。随意完成任务。

© www.soinside.com 2019 - 2024. All rights reserved.