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

Posted on 2008-11-09
Last Modified: 2012-06-22

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?
Question by:JaimeJegonia
    LVL 14

    Expert Comment

    You can use the sysinternals tool filemon...

    The explanation/use is in the link.
    LVL 85

    Accepted Solution

    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.
    LVL 14

    Expert Comment

    Idle_Mind -

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

    Author Comment


    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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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...?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now