I am having issues where a program is copying data from one record to the following record in an old VB6 random access file. This happens when it reads the data, but only sometimes. I have also noticed that a VB .Net library that does the same things with file streams does not have this problem. This has only just started happening, this code hasn’t been changed recently.
Here is the relevant code. This should just read one record, and not change anything about the file.
VB6:
Property Let Location(ByVal Source As String)
FileName = Source + ShortFileName
FileNumber = FreeFile
Open FileName For Random Access Read Write Shared As FileNumber Len = 200
FileOpen = True
End Property
Public Sub Read(ByVal RecordNumber As Long)
Get FileNumber, RecordNumber, FileData()
RecordLocked = False
End Sub
VB .Net:
public string Location
{
set
{
FileName = value + ShortFileName;
strOriginalFilePath = value;
fs = new FileStream(FileName, FileMode.Open, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite, BufferLength, FileOptions.RandomAccess);
IsFileOpen = true;
}
}
public void Read(long RecordNumber)
{
checked
{
bool flag = default(bool);
while (!flag)
{
try
{
fs.Position = (RecordNumber - 1) * BufferLength;
fs.Read(FileData, 0, BufferLength);
flag = true;
}
catch (Exception projectError)
{
ProjectData.SetProjectError(projectError);
Application.DoEvents();
ProjectData.ClearProjectError();
}
}
}
}
2