FileStream.Lock Method (Int64, Int64)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Prevents other processes from reading from or writing to the FileStream.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

abstract Lock : 
        position:int64 *
        length:int64 -> unit
override Lock : 
        position:int64 *
        length:int64 -> unit


Type: System.Int64

The beginning of the range to lock. The value of this parameter must be equal to or greater than zero (0).

Type: System.Int64

The range to be locked.

Exception Condition

position or length is negative.


The file is closed.


The process cannot access the file because another process has locked a portion of the file.

Locking a range of a file stream gives the threads of the locking process exclusive access to that range of the file stream.

For a list of common file and directory operations, see Common I-O Tasks.

The following code example demonstrates how to lock part of a file so another process cannot access that part of the file even though it has read/write access to the file. Run the program simultaneously in different command windows and investigate using the different console input options.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top