[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 891
  • Last Modified:

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

Hi,

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?
0
JaimeJegonia
Asked:
JaimeJegonia
  • 2
  • 2
1 Solution
 
top_rungCommented:
You can use the sysinternals tool filemon...

http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx

The explanation/use is in the link.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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:
http://msdn2.microsoft.com/en-us/library/system.io.fileshare.aspx

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

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

            fs.Close()
        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.
0
 
top_rungCommented:
Idle_Mind -

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

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

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

        Return result
    End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now