简体   繁体   English

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

[英]Open old VB6 Random Access File using C#

I'm trying to open old VB6 files which were created using random access. 我正在尝试打开使用随机访问创建的旧VB6文件。

The Type used was as follows: 使用的类型如下:

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

And access was as follows: 并访问如下:

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

Now I want to open this file using C# and import all the data in a SQL datatable. 现在,我想使用C#打开此文件,并将所有数据导入SQL数据表中。 Could anyone help me to open this file using the Type CDB as structure? 有人可以帮我使用CDB型结构打开此文件吗?

To use my sample, you have make an alias for the Microsoft.VisualBasic.Filesystem Assembly: 要使用我的示例,您需要为Microsoft.VisualBasic.Filesystem程序集创建别名:

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

Then within your code: 然后在您的代码中:

// 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);

Of course you have to analyze the old record structure (vb6 knows "fixed length strings", while C# do not really know them ...) and how the data are represented in the file. 当然,您必须分析旧的记录结构(vb6知道“固定长度的字符串”,而C#并不真正知道它们……)以及如何在文件中表示数据。 Maybe you should read a Byte Array in c# handle the binary data (like dates, numbers ...) "by hand". 也许您应该在c#中读取“手动”处理二进制数据(如日期,数字...)的字节数组。

What I did not try is the FileGetObject method for using a byte array a referenced variable. 我没有尝试过的是使用字节数组作为引用变量的FileGetObject方法。 Feel free to complete the task. 随意完成任务。

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

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