title: .net file random access recoard locking
body: <p><strong>Summary:</strong> In <strong>.net</strong> When locking a record in a random access file we cannot access records ahead of the locked record in the file.</p>
  
  <p>To demonstrate the issue I have written two simple programs one opens and locks a record and the other tries to read through.</p>
  
  <p>The results are that when locking record number 9 out of the 10 in the first program we are able to read records 1 and 2 but no more! The expectation (And this is our experience with VB6) is that you should be able to read all the records apart from the one you have locked.</p>
  
  <p>Has anyone seen this problem? Am I doing something strange? Any work around?</p>
  
  <p><strong>Demo Code:</strong> </p>
  
  <p><strong>Program 1 Create/Open/Lock</strong></p>
  
  <pre><code>Sub Main()
  
      Dim FileName As String = "test.a"
      Dim ListofName() As String = {"Name1", "Name2", "Name3", "Name4",
              "Name5", "Name6", "Name7", "Name8", "Name9", "Name10"}
  
      Try
  
          Dim FileNumber1 As Integer = FreeFile()
          FileOpen(FileNumber1, FileName, OpenMode.Random,
                   OpenAccess.ReadWrite, OpenShare.Shared, 600)
  
          FileGet(FileNumber1, People, 1)
  
          'Create File if needs be
          If People.Name = "" Then
              For A = 1 To 10
                  People.Name = ListofName(A - 1)
                  FilePut(FileNumber1, People, A)
              Next
          End If
  
          'Lock the recoard we want for testing
          Lock(FileNumber1, 9)
  
      Catch ex As Exception
          FileClose()
      End Try
      FileClose()
  End Sub
  </code></pre>
  
  <p>_</p>
  
  <p><strong>Program 2 Open and try and read</strong></p>
  
  <pre><code>Sub Main()
  
      Dim FileName As String = "C:\**Location of first program file**\test.a"
  
      Try
  
          Dim FileNumber1 As Integer = FreeFile()
          FileOpen(FileNumber1, FileName, OpenMode.Random,
                   OpenAccess.ReadWrite, OpenShare.Shared, 600)
  
          FileGet(FileNumber1, People, 2)
  
          'See  how much of the file we can read
          For A = 1 To 10
              FileGet(FileNumber1, People, A)
              System.Diagnostics.Debug.WriteLine(People.Name.ToString)
          Next
  
      Catch ex As Exception
          FileClose()
      End Try
      FileClose()
  End Sub
  </code></pre>
  
  <p>Edit 0.1: We have found that the deeper an individual record is locked within the file the more bytes/records are inaccessible before the locked one.</p>