Jump to content

Visual Basic File Lock Unlock

- - - - -

  • Please log in to reply
No replies to this topic

#1
QuackWare

QuackWare

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Visual Basic lock and unlock a file




/** This code snippet was taken from freecodesnippets.com*/

 


Imports System.IO


 


Module Module1


 


Sub Main()


Dm 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










1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users