1

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?

  • What does the content look like? Is it binary? Is it Text? What encoding if Text? ... – Fildor Feb 12 '19 at 11:58
  • 4
    Would it be feasable to write a little VB6 code that transposes the file's content to JSON content? Or XML? Or do you need to use the file directly? – Fildor Feb 12 '19 at 11:59
  • That code _should_ be directly translatable to C# - it's not exactly complex. Watch out for the VB 'Date' information being different to the .NET 'DateTime' though? – Grim Feb 12 '19 at 12:10
  • The "old" style File IO is "hidden" in one of the "Microsoft.VisualBasic...." Assemblies (working really well, including the ability to handle file sizes > 4gb ...) – nabuchodonossor Feb 12 '19 at 13:26
  • Microsoft.VisualBasic.FileSystem is the Namespace for Open, close, get, put. I´ll put a sample here in a few minutes. – nabuchodonossor Feb 12 '19 at 13:31
  • Check this answer to a similar question https://stackoverflow.com/a/9890311/15639 – MarkJ Feb 12 '19 at 14:31
  • 1
    Possible duplicate of [Reading (with Filesystem.FileGet) VB6 record file (written with Put) with C#](https://stackoverflow.com/questions/9889477/reading-with-filesystem-fileget-vb6-record-file-written-with-put-with-c-shar) – StayOnTarget Feb 12 '19 at 20:18
  • Content looks like this: " Àœã@  Ÿã@L.C. Guillemin & Co. Ltd Louis Jose Clency Guillemin 106, Route des Pamplemousses, Sainte Croix 242-2021 20217445C07026888" As you can see, the dates are in some kind of weird format. – Jedi Master Feb 13 '19 at 08:50

1 Answers1

2

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.

nabuchodonossor
  • 2,095
  • 20
  • 18
  • I've tried your code above in vain. C# only uses "using ..." and not Imports. Also under Microsoft.VisualBasic namespace there is no FileSystem. This is VStudio 2017. – Jedi Master Feb 13 '19 at 08:25
  • I would highly recommend to avoid any dependencies to anything VB6 for the "new" Software. To me this looks like a job for a data convertion project. – Fildor Feb 13 '19 at 09:25
  • @Fildor: This is not REAL VB6, its .NET (but hidden in the "Microsoft.VisualBasic.*" namespaces. – nabuchodonossor Feb 13 '19 at 10:21
  • I am aware of that. Nevertheless I'd consider importing that namespace as _very_ unclean conduct in a C# project. – Fildor Feb 13 '19 at 10:52
  • @Fidor: Please explain this further: Why do you think that I should not use a Microsoft Product? Because of the "Basic" in the name? Are you a VB hater? – nabuchodonossor Feb 13 '19 at 11:15
  • Still the namespace Microsoft.VisualBasic.* does NOT contain anything like FileSystem – Jedi Master Feb 13 '19 at 11:17
  • @JediMaster: If you read the sample, you will see that you need Microsoft.VisualBasic.FileSystem for the Filesystem Methods, and the Microsoft.VisualBasic for the Enums. – nabuchodonossor Feb 13 '19 at 11:18
  • 1
    Have added a new Visual Basic project to my solution where I could make use of the example. Thank you! – Jedi Master Feb 14 '19 at 12:06
  • @nabuchodonossor No, not a hater. I just tend to avoid mixing water and oil. – Fildor Feb 14 '19 at 15:32
  • @Fildor: Thanks for your answer, I see your point. Nevertheless, the name of the assembly let us assume, it would be part of basic, but in reality, it is part of the .NET universe. You can use this from whatever .NET language you want to. And further, basically the "FileSystem" Assembly is just a wrapper to avoid your own windows Api calls (which would be also possible to handle fixed length records). So no fear needed. – nabuchodonossor Feb 15 '19 at 07:13