FileSystemWatcher VB.Net How to check if File is opened by application

reredok
reredok used Ask the Experts™
on
Hi Experts.

how can I check if File is opened by application in VB.Net?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You can attempt to open the file in exlusive read/write mode.  If the file is opened by anything else, the attempt will fail:
    Private Function IsFileInUse(ByVal fileName As String) As Boolean
        Try
            Dim fs As System.IO.FileStream
            fs = New FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            fs.Close()
            Return False
        Catch ex As Exception
            Return True
        End Try
    End Function

Open in new window

reredokIT Consultant

Author

Commented:
it works :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial