4

Does anyone know if it is possible to read random access files in C#?

I am trying to replicate the following function (from an old VB6 application) in C# -

Open File For Random Shared As #100 Len = Len(Record)
    Get #100, DM, Record
Close #100

Public DM As Long
Public Record As DMrecord

Public Type DMrecord
column1 As Long
column2 As Integer
column3 As Integer
column4 As Integer
column5 As String * 4
End Type

EDIT -

I have now tried using the VisualBasic DLL as suggested below and receive the following error on the FileGetObject line -

"The best overloaded method match for Microsoft.VisualBasic.FileSystem.FileGetObject(int, ref object, long) has some invalid argument"

The code I am using is -

        public class Record 
    {
        public int DMtype;
        public long ecn;


        public Record(int DMtype, long ecn) 
        {
            this.DMtype = DMtype;
            this.ecn = ecn;
        }

        public Record()
        {
        }
    }


string fileName = @"C:\RandomAccess.dat";
        string returnString = string.Empty;
        int row = 1;
        int maxRow = 1000;

        Record aFileRecord = new Record();

        FileSystem.FileOpen(1, fileName, OpenMode.Random, OpenAccess.Read, OpenShare.LockRead);

        while (row < maxRow)
        {
            //Get record 2 1st.>>
            FileSystem.FileGetObject(1, aFileRecord, row);
            returnString += aFileRecord.DMtype.ToString() + "$" + aFileRecord.ecn.ToString();
            row++;
        }

        FileSystem.FileClose(1);

I have tried setting 'Record' as both a struct and a class and get the same error.

EDIT 22/08/13 - I never did get to the bottom of this, ended up exporting the random access data to comma seperated text files in VB6, then consuming the files in SSIS.

user1948635
  • 1,357
  • 4
  • 15
  • 22
  • 3
    You will have to interpret the data yourself using BinaryReader. [See here to get started](http://stackoverflow.com/questions/4429829/read-variable-sized-string-from-binary-file-vb6-vs-c) Long you can read with `BinaryReader.ReadInt64()`, Integer with `BinaryReader.ReadInt32()`, and the strings you will have to read one at a time. Read them as described in the link I gave. – Matthew Watson Apr 30 '13 at 16:40
  • note: vb6 Long was actually int32 – John Sobolewski Apr 30 '13 at 16:45
  • Oops, so I should have said "Long you can read with `BinaryReader.ReadInt32()`" – Matthew Watson Apr 30 '13 at 16:46
  • Try http://stackoverflow.com/q/265639/17776 – jac Apr 30 '13 at 21:28
  • 2
    Just a guess, `object aFileRecord = new Record()` and `FileSystem.FileGetObject(1, ref aFileRecord, row)` – jac May 01 '13 at 16:56

1 Answers1

1

Just add a reference to Microsoft.VisualBasic.dll and use FileSystem.FileOpen specifying Random open mode, and the FileSystem.FileGetObject method. This behaves the same as the Open statement and Get keyword in VB6.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • Thanks for the advice, I'll have a look into the VisualBasic.dll now. – user1948635 May 01 '13 at 13:33
  • Hmm, can't quite get this working. The FileSystem.FileGetObject(1, aFileRecord, 1) line is throwing an error when I try to pass in a new instance of a struct (aFileRecord in the line above). Any idea what I am doing wrong? – user1948635 May 01 '13 at 14:35
  • Just to add, the error is - "The best overloaded method match for Microsoft.VisualBasic.FileSystem.FileGetObject(int, ref object, long) has some invalid arguments" – user1948635 May 01 '13 at 14:55
  • Perhaps it doesn't like structs. You could try using a class instead?? (Disclaimer - I'm recommending these techniques based on the documentation, and haven't actually used them myself.) – MarkJ May 01 '13 at 15:10
  • I have tried both a class and a struct, both receiving the same error, see the question for an edit containing updated code. – user1948635 May 01 '13 at 16:21
  • 1
    @user1948635 You have forgotten to put *ref aFileRecord*. The method expects a reference to an object there, not the object itself. – ThunderGr Oct 05 '13 at 08:42