Jedi Master
Jedi Master

Reputation: 59

Open old VB6 Random Access File using C#

I'm trying to open old VB6 files which were created using random access.

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. Could anyone help me to open this file using the Type CDB as structure?

Upvotes: 1

Views: 680

Answers (1)

nabuchodonossor
nabuchodonossor

Reputation: 2060

To use my sample, you have make an alias for the Microsoft.VisualBasic.Filesystem Assembly:

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. Maybe you should read a Byte Array in c# handle the binary data (like dates, numbers ...) "by hand".

What I did not try is the FileGetObject method for using a byte array a referenced variable. Feel free to complete the task.

Upvotes: 2

Related Questions