C# FileStream Unlock

Description

FileStream Unlock Allows access by other processes to all or part of a file that was previously locked.

Syntax

FileStream.Unlock has the following syntax.


public virtual void Unlock(
  long position,
  long length
)

Parameters

FileStream.Unlock has the following parameters.

  • position - The beginning of the range to unlock.
  • length - The range to be unlocked.

Returns

FileStream.Unlock method returns

Example


/*from   ww w. j a va  2  s.c o m*/
using System;
using System.IO;
using System.Text;

class FStreamLock
{
    static void Main()
    {
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        string myData = "from java2s.com ";
        int textLength = uniEncoding.GetByteCount(myData);
        int recordNumber = 13;
        int byteCount = uniEncoding.GetByteCount(recordNumber.ToString());

        using(FileStream fileStream = new FileStream("Test.dat", FileMode.OpenOrCreate, 
            FileAccess.ReadWrite, FileShare.ReadWrite)){
     
                fileStream.Lock(textLength - 1, byteCount);
                Console.WriteLine("The specified part " +
                    "of file has been locked.");

                fileStream.Unlock(
                    textLength - 1, byteCount);
                Console.WriteLine("The specified part " +
                    "of file has been unlocked.");
     
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter