Share via


Lock, Unlock Functions 

Controls access by other processes to all or part of a file opened using the Open function.

The My feature gives you greater productivity and performance in file I/O operations than Lock and Unlock. For more information, see My.Computer.FileSystem Object.

Public Overloads Sub Lock(ByVal FileNumber As Integer)
' -or-
Public Overloads Sub Unlock(ByVal FileNumber As Integer)
' -or-
Public Overloads Sub Lock(_
   ByVal FileNumber As Integer, _ 
   ByVal Record As Long _
)
' -or-
Public Overloads Sub Unlock( _
   ByVal FileNumber As Integer, _ 
   ByVal Record As Long _
)
' -or-
Public Overloads Sub Lock( _
   ByVal FileNumber As Integer, _ 
   ByVal FromRecord As Long, _ 
   ByVal ToRecord As Long _
)
' -or-
Public Overloads Sub Unlock( _
   ByVal FileNumber As Integer, _
   ByVal FromRecord As Long, _
   ByVal ToRecord As Long _
)

Parameters

  • FileNumber
    Required. Any valid file number.
  • Record
    Optional. Number of the only record or byte to lock or unlock
  • FromRecord
    Optional. Number of the first record or byte to lock or unlock.
  • ToRecord
    Optional. Number of the last record or byte to lock or unlock.

Exceptions

Exception type Error number Condition

IOException

52

FileNumber does not exist.

IOException

54

File mode is invalid.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

The Lock and Unlock functions are used in environments where several processes might need access to the same file.

Lock and Unlock functions are always used in pairs. The arguments to Lock and Unlock must match exactly.

If Record, or FromRecord and ToRecord are not supplied, the lock will be for the entire file. If Record alone is specified the single record will be locked/unlocked.

If the file has been opened for sequential input or output, Lock and Unlock affect the entire file, regardless of the range specified by FromRecord and ** ToRecord.

Example

This example illustrates the use of the Lock and Unlock functions. This example assumes that People.txt is a file containing records of the structure Person.

Structure Person
   Dim Name As String
   Dim ID As Integer
End Structure

Sub PutInLockedFile(ByVal index As Integer, ByVal onePerson As Person)
   Try
      FileOpen(1, "c:\people.txt", OpenMode.Binary)
      Lock(1)
      FilePut(index, onePerson)
      Unlock(1)
      FileClose(1)
   Catch
      ' Error recovery code here.
   End Try
End Sub

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

FileOpen Function
IOException Class

Other Resources

File Access with Visual Basic