Lock and unlock a file : File Lock « File Directory « VB.Net





Lock and unlock a file

Lock and unlock a file
Imports System.IO

Module Module1

    Sub Main()
        Dim FileSt As FileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write, FileShare.Write)

        Try
            FileSt.Lock(0, 100)
            Console.WriteLine("Locked")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

        Try
            FileSt.Unlock(0, 100)
            Console.WriteLine("Unlocked")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

        Dim Values As Byte()
        Values = New Byte() {1, 2, 3, 4, 5}

        Try
            FileSt.Seek(0, SeekOrigin.Begin)
            FileSt.Write(Values, 0, 5)
            Console.WriteLine("Successfully updated file")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

    End Sub

End Module


           
       










Related examples in the same category