VB - Check if file is in use

By , 11/10/2007
(1 ratings)
This function checks if a given file is in use by opening the file with exclusive read/write permissions. If the file is in use this function wont get exclusive permissions and will throw an exception and the function will return "true", otherwise you'll get "false"
Public Function FileInUse(ByVal sFile As String) As Boolean
    If System.IO.File.Exists(sFile) Then
        Try
            Dim F As Short = FreeFile()
            FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
            FileClose(F)
        Catch
            Return True
        End Try
    End If
End Function

Tagged with File.

Comments

 

Log in, to comment!