Export (0) Print
Expand All

FileSystem.Lock Method (Int32)

Controls access by other processes to all or part of a file opened by using the Open function. The My feature gives you better productivity and performance in file I/O operations than Lock and Unlock. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void Lock(
	int FileNumber
)

Parameters

FileNumber
Type: System.Int32

Required. Any valid file number.

ExceptionCondition
IOException

FileNumber does not exist.

IOException

File mode is invalid.

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 be identical.

If Record, or FromRecord and ToRecord are not supplied, the lock will be for the whole 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 whole file, regardless of the range specified by FromRecord andToRecord.

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

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

Sub PutInLockedFile(ByVal onePerson As Person)
    FileOpen(1, "c:\people.txt", OpenMode.Binary)
    Lock(1)
    FilePut(1, onePerson)
    Unlock(1)
    FileClose(1)
End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft