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.

5
  • 3
    You will have to interpret the data yourself using BinaryReader. See here to get started 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. Apr 30, 2013 at 16:40
  • note: vb6 Long was actually int32 Apr 30, 2013 at 16:45
  • Oops, so I should have said "Long you can read with BinaryReader.ReadInt32()" Apr 30, 2013 at 16:46
  • Try stackoverflow.com/q/265639/17776
    – jac
    Apr 30, 2013 at 21:28
  • 2
    Just a guess, object aFileRecord = new Record() and FileSystem.FileGetObject(1, ref aFileRecord, row)
    – jac
    May 1, 2013 at 16:56

1 Answer 1

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.

6
  • Thanks for the advice, I'll have a look into the VisualBasic.dll now. May 1, 2013 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? May 1, 2013 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" May 1, 2013 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 1, 2013 at 15:10
  • 1
    @user1948635 You have forgotten to put ref aFileRecord. The method expects a reference to an object there, not the object itself.
    – ThunderGr
    Oct 5, 2013 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.