knowing when file has been totally transferred

G

Guest

let's say I'm transferring a large file like 100MB over to a folder.

The program detects when the file arrives. However, I can't
figure out how to know when the file is totally transferred over.

One unsuccessful method is to...

dim fi as new fileinfo(newfile)

fi.length returns the supposed size of the file. But it correctly
reports the final size of the file the moment transferring
starts. So that's no help at all.

I want to know what the file is totally transferred to the folder
so I can then begin to work on it. Any ideas?
 
M

Michel Posseth [MCP]

Well i would do the following

i would use a filesystem watcher to detect the arrival of the file (
Created event ) this event will fire as soon as the transfer starts
however it will also provide us the location and name of the file so we can
wait in a loop untill we can get exclusive access to the file
when this is possible we know that the file is completely transfered

regards

Michel Posseth [MCP]
 
G

Guest

uhm..... what method do you use when you wanna find out if we get exclusive
access to the file?

Michel Posseth said:
Well i would do the following

i would use a filesystem watcher to detect the arrival of the file (
Created event ) this event will fire as soon as the transfer starts
however it will also provide us the location and name of the file so we can
wait in a loop untill we can get exclusive access to the file
when this is possible we know that the file is completely transfered

regards

Michel Posseth [MCP]



chad said:
let's say I'm transferring a large file like 100MB over to a folder.

The program detects when the file arrives. However, I can't
figure out how to know when the file is totally transferred over.

One unsuccessful method is to...

dim fi as new fileinfo(newfile)

fi.length returns the supposed size of the file. But it correctly
reports the final size of the file the moment transferring
starts. So that's no help at all.

I want to know what the file is totally transferred to the folder
so I can then begin to work on it. Any ideas?
 
M

Michel Posseth [MCP]

open the file in a try catch block and use
FileShare.None as the share parameter to a FileStream object

you might wrap this in a nice function



regards

Michel Posseth [MCP]


chad said:
uhm..... what method do you use when you wanna find out if we get
exclusive
access to the file?

Michel Posseth said:
Well i would do the following

i would use a filesystem watcher to detect the arrival of the file (
Created event ) this event will fire as soon as the transfer starts
however it will also provide us the location and name of the file so we
can
wait in a loop untill we can get exclusive access to the file
when this is possible we know that the file is completely transfered

regards

Michel Posseth [MCP]



chad said:
let's say I'm transferring a large file like 100MB over to a folder.

The program detects when the file arrives. However, I can't
figure out how to know when the file is totally transferred over.

One unsuccessful method is to...

dim fi as new fileinfo(newfile)

fi.length returns the supposed size of the file. But it correctly
reports the final size of the file the moment transferring
starts. So that's no help at all.

I want to know what the file is totally transferred to the folder
so I can then begin to work on it. Any ideas?
 
C

Chris Dunaway

Another option is to save the file using a temporary name and then
rename it to the final destination name. That way the
FileSystemWatcher would key on the rename event and you would catch the
file after it was completely transferred.
 
S

Stephany Young

There may be others but one sure-fire way to determine if a file is
available for use is to attempt to open it for exclusive access.

If the attempt fails then it is NOT available.

If the attempt succeeds then it IS available.

The sort of logic you need to do this is (pseudo-code):

Catch the 'Create' event from FileSystemWatcher

While True
Try
Attempt to open the file for exclusive use
....
If the logic gets to here then it succeeded so do whatever you want to
do with the file
....
Exit While
Catch ex As Exception
Check the exception
If it is not the exception that tells us that the file is not
available then deal with it
....
Exit While
End If
If the logic gets to here then the file is not available so allow the
loop to continue
End Try
End While
 
W

William LaMartin

I have used the code below to determine when a file upload is finished
before making a thumbnail of the file. It has always worked for me. It
appears that the file.exists does not equal to true until the file has been
totally uploaded.


Dim fi As New System.IO.FileInfo(Server.MapPath(".") & "\Photos\" &
FileUpload1.FileName)
Do While fi.Exists = False
'do nothing
Loop
 

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