Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET]FileAccessible

originally posted on dotnet-snippets.com

Description:

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"

Code
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

If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3
PHP & .NET Contributor.
Just for future reference, FileAccessible would be a better suited name for this function since it will also return true if your application is not allowed to access sFile.
(May 1, 2010 09:08 PM)Zer0Fact0r Wrote:  Just for future reference, FileAccessible would be a better suited name for this function since it will also return true if your application is not allowed to access sFile.

ok renamed it
PHP & .NET Contributor.