Reading a random access file in C #

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 -

Now I tried using the VisualBasic DLL as suggested below and getting the following error in the FileGetObject line -

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

The code I use 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 tried to set the "Record" to both the structure and the class and get the same error.

EDIT 22/08/13 . I did not get to the end, as a result, exported random access data to split text files in VB6, and then consumed the files in SSIS.

+4
source share
1 answer

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

+1
source

All Articles