How do you detect when a file is finished being created

G

Guest

Hello,

I could use a little help from you Gurus out there. I have an aplication
that watches a directory and detects when a PGP encrypted file lands in the
directory and starts a process that decrypts it and parses the resulting
contents. I am using the FileSystemWatcher's Created event to detect when
the file arrives. The problem I have is that these files are large(5-10
megs) and they are being transfered over a limited pipe. I don't want to
start the decryption and parsing until they are done downloading but I can't
seem to find a straightforward way of sorting out when that is. The
FileSystemWatcher Created event fires off as soon as the file starts to hit
the directory and it kicks off my other processes to early.

Does anyone have a way to determine when the file is done loading? (note -
I have no idea how long the file will take to download or how big it may be.)

Thanks for any help,
Doug
 
J

Jim Douglas

On the old unix systems I would check the filesize in a loop, filesize ==
same after a few times I figured the file was complete. I'm sure .NET has
some class, methods to do this?
 
G

Guest

I was hoping to avoid that aproach if I could. Part of the problem with it
is that the downloads can temporarily stall causing a false positive. That
would also require me to build it as a multithreaded system and it is quite
happily single threaded at the moment.
 
P

Patrice

Not familiar with it but what if you check the changed event ? Also once you
have no more changed event for a period of time you could perhaps try to
open the file with an exclusive lock for an additonal check.

Patrice
 
P

petterl

Hi Doug

I ran into the same problem in my filewatcher. I made a function that
checked if the file was open
if the function was true then it waited and then tried again until the file
was ready.

Private Function isFileOpen(ByVal filename As String) As Boolean

Dim sf As System.IO.FileStream

Try

sf = System.IO.File.Open(filename, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)

Return False

Catch ex As System.IO.IOException

Return True

ex = Nothing

Finally

'

If Not IsNothing(sf) Then

sf.Close()

sf = Nothing

End If



End Try

End Function



And in the code do an DO loop as follow



Do

System.Threading.Thread.Sleep(500)

Loop While wfile.Exists(file_name) And isFileOpen(file_name)



I hope this helps



Petter L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top