File locking problem

A

Aaron Smith

I have a small application that runs in the taskbar. All it has is a
timer that goes off every second. It looks at a list of files on a
source and then on a destination folder. If it can't find a file in the
source folder in the destination folder, it copies it into the
destination. Everything is working fine, except for one thing... There
is another application that creates the files in the source folder.
This application creates a file in the source folder, then it fills the
file with data...

The application that creates the file locks up until the copy
application is closed.. I've tried setting the timer to every 30
seconds, but it still locks up.

Is there a way to check to see if a file is in use first and then just
skip that file?

Here is some code that I use:

SourceFiles = System.IO.Directory.GetFiles(txtSource.Text)
DestFiles = System.IO.Directory.GetFiles(txtDestination.Text)
Dim iCount As Integer
For iCount = 0 To SourceFiles.Length - 1
bFound = False
Dim iCountDest As Integer
For iCountDest = 0 To DestFiles.Length - 1
If System.IO.Path.GetFileName(DestFiles(iCountDest)) =
System.IO.Path.GetFileName(SourceFiles(iCount)) Then bFound = True
Next
If bFound = False Then
sourcePath = SourceFiles(iCount)
destPath = txtDestination.Text & "\" &
System.IO.Path.GetFileName(SourceFiles(iCount))

System.IO.File.Copy(sourcePath, destPath)
End If
Next

Thanks,
Aaron
 
R

Ray Cassick \(Home\)

Look into the FileSystemWatcher instead of the timer. It is made to fix
problems like this.
 

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