How dou detect if a file is being access by a process


I am downloading a file from an ftp server and move it afterwards to a separate folder. But I am getting this error: The process cannot access the file because it is being used by another process. I am thinking that file-download was not yet finished when the move was done. How do I know if a file is being access or not by any process?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the sysinternals tool filemon...

The explanation/use is in the link.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think he means programmatically check...   =)

Attempt to open the file with exclusive access...if it fails then someone else has it open.

See IO.FileShare.None:

            Dim fs As New System.IO.FileStream("c:\someFile.txt", IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None)

            ' do something with "fs" have exclusive access to it
            ' and NO one else has it open (or can open it while you are working with it)

        Catch ex As Exception
            ' could not get read/write lock, file IS in use by someone else
        End Try

You could use a Timer and attempt again after a couple seconds if it fails.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Idle_Mind -

I think you are right!! ;)   FAIL!  Didn't mind the zone - sorry.
JaimeJegoniaAuthor Commented:

I already have that function but it seems that my code is still able to move the file because I can see files with size 0kb on the destination folder. When I put a delay before moving, it worked. But I'm afraid that this approach would result to a bottleneck later. Any suggestions?

Here's the code:
    Public Function IsLock(ByVal filenameAs String) As Boolean
        Dim result As Boolean = False
        Dim fs As System.IO.FileStream

            fs = System.IO.File.Open(filenameAs , IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None)
        Catch ex As System.IO.IOException
            result = True
        End Try

        Return result
    End Function
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
The difference may be that you are using IO.FileAccess.Read instead of IO.FileAccess.ReadWrite.  You are saying, "open this file for reading and don't allow any other processes to read it at the same time".

The other process isn't reading (it's just writing) so it might not throw the error...?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.